【问题标题】:Update a nested form更新嵌套表单
【发布时间】:2016-09-13 22:20:39
【问题描述】:

我是 RoR 的新手,我正在尝试构建一个 Web 应用程序。 我有一个经典应用,用户拥有 Post。

另一个模型 Online 用于将帖子放在公共墙上,它与表示可用件的嵌套表单 Orders 相关联。

所以现在,我正在尝试在我的帖子显示视图中使用“已采取”的操作来更新订单,但是 rails 说他找不到带有 'id'= 的帖子,并且使用了在线设置的私有方法,该方法正在工作创建订单。 (照片)

错误: 我的代码:

在线控制器:

class OnlinesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_post 
  before_action :owned_online, only: [:new, :update]
  before_action :set_online, except: [:taked]
  before_action :set_unline, only: [:taked]


  def new 
    @online = current_user.onlines.build
    @online.post_id = @post.id
    @online.user_id = current_user.id
  end 

  def edit
  end

   def taked 
  @online.orders.update(taked: false, taked_at: Time.zone.now, taked_by: current_user)
end

  def create 
      if Online.where(post_id: params[:post_id]).any?
      @online = Online.where(post_id: params[:post_id]).last.update_attributes(push: false)
      end
     @online = @post.onlines.create(online_params)
    if @online.save
      if @online.portion <= 0
          @online.update(push: false)
          flash[:success] = 'Veuillez indiquer le nombre de parts disponibles '
          redirect_to root_path 
        else
       @online.update(pushed_at: Time.zone.now)
       @online.update(push: true)

       
       flash[:success] = 'Votre post est en ligne !'
      redirect_to root_path
    
    end
    else 
      render 'new'
    end 
  end 




def update  
    if @onlines.update(online_params)
      if @online.push == false
        if @online.portion <= 0
          @online.update(push: false)
          flash[:success] = 'Veuillez indiquer le nombre de parts disponibles '
          redirect_to root_path 
        else
         @online.update(push: true)
         flash[:success] = 'Votre post a bien été pushé !'
         redirect_to root_path      
      end   
    end
    else
      @user.errors.full_messages
      flash[:error] = @user.errors.full_messages
      render :edit
    end
  end


private 

def online_params
  params.require(:online).permit(:user_id, :post_id, :prix, :portion, :push, :pushed_at, orders_attributes: [:id, :taked, :taked_at, :taked_by, :validated_at, :validated_by, :_destroy])
  end 

  def owned_online 
     @post = Post.find(params[:post_id])
  unless current_user == @post.user
    flash[:alert] = "That post doesn't belong to you!"
    redirect_to :back
  end
end  

  def set_post
  @post = Post.find_by(params[:post_id]) 
  end 


  def set_online
    @post = Post.find(params[:post_id])
    @online = Online.find_by(params[:id]) 
  end 

  def set_unline
  @online = Online.find_by(params[:id]) 
end

end

class Online < ActiveRecord::Base
  
  belongs_to :post
  belongs_to :user
  has_many :orders
  
  accepts_nested_attributes_for :orders, allow_destroy: true
  
  scope :push, ->{ where(push: true).order("pushed_at DESC") }
end

查看/帖子/节目:

 <div class="btn-group" role="group" aria-label="...">
  <%= link_to '-  Pusher  - ', new_post_online_path(@post), data: { confirm: 'Confirmer la mise en ligne de #{@title}?' }, class: "btn btn-primary " %>


  <div class="col-md-9">
    <h3>Parts :</h3>
    <div id="Orders">
      
        <ul>
    <%- @post.onlines.each do |online| %>
      <%- online.orders.each do |order| %>
      <%- if order.taked == false %>
      <li>
  <%= link_to 'Take', taked_online_path(online), method: :update, class: "btn btn-warning"%>
      </li>
      <%end%>
      <%end%>
    <%end%>
  </ul>
    </div>
  </div>

还有路线:

Rails.application.routes.draw do
  get 'profiles/show'

  mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
  
  devise_for :users, :controllers => { registrations: 'registrations' }

  resources :posts do 
    resources :comments
    resources :onlines do 
      resources :orders
    end
end

  get ':pseudo', to: 'profiles#show', as: :profile
  get ':pseudo/edit', to: 'profiles#edit', as: :edit_profile
  patch ':pseudo/edit', to: 'profiles#update', as: :update_profile

 put 'online/:id/taked', to: 'onlines#taked', as: :taked_online

  
  

  root 'posts#index'

所以,如果您对此有任何建议,我会接受的! 谢谢

【问题讨论】:

  • 您究竟从哪里得到这个Couldn't find Post with 'id'= 错误?
  • 进入我的在线控制器,在私有方法中
  • 我认为问题出在那一行: 但我不知道t 找到如何更新属性
  • 你能用完整的错误堆栈和服务器日志中生成的参数来更新你的问题吗?
  • 我给你一张错误页面的图片,上面有所有信息,以及我忘记的路线

标签: javascript jquery mysql ruby-on-rails ruby


【解决方案1】:

找不到带有 'id'= 的帖子

您有before_action :set_online,它在params{:post_id] 的帮助下分配@post,然后该控制器的任何操作被触发。但是没有 post_id 用于 taked 操作,因此它失败报告该错误。您可以通过将before_action :set_online 更改为before_action :set_online, except: [:taked] 来避免此检查,但这又不会分配 @online 用于taked 操作这是该方法所必需的

因此,您唯一的选择是删除分配 @postset_online 方法中。

def set_online
  @online = Online.find_by(params[:id]) 
end

此外,您的代码需要进行一些更改。首先,taked_online路由 是错误的。应该是

put 'online/:id/taked', to: 'onlines#taked', as: :taked_online

因为您使用它来更新,而不是创建

最后,taked 方法需要调整。

def taked 
  @online.orders.update(taked: false, taked_at: Time.zone.now, taked_by: current_user)
end

【讨论】:

  • 感谢您的回复,我已经采纳了您的所有建议,我想我离目标还很远。我复制了 set_online 方法以删除@post,因为我提出。但是,更改后的路由仍然存在错误,错误提示“没有路由匹配 [POST]”/online/248/taked“”。那么为什么他认为我要在我的代码中创建一个新的在线?
  • @lilipupu 你是否按照我的更改更改了路线?
  • 是的,它现在已经放入,但错误仍在 POST 中
  • @lilipupu 尝试重新启动服务器。
  • 嗯,现在,我在使用 GET 时遇到了同样的错误。所以我尝试使用 PATCH 但同样的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多