param缺失或值为空:植物

时间:2016-05-13 00:16:35

标签: ruby-on-rails ruby ruby-on-rails-4

我在我的控制器中添加了一个名为transplant的自定义操作。我只是想渲染一个下拉表单,根据' tray_id'

选择位于何处

我的路线看起来像这样:

resources :plants do
    member do
      get 'transplant'
    end
    resources :plantdats, :plant_cycles, :tasks
 end

我的控制器看起来像这样:

before_action :set_plant, only: [:show, :edit, :update, :destroy, :transplant]

def transplant

  if @plant.update(plant_params)
    redirect_to @plant
    flash[:success] = "Transplanted successfully"
  end
end
def set_plant
    @plant = Plant.find(params[:id])
end

    # Never trust parameters from the scary internet, only allow the white list through.
def plant_params
   params.require(:plant).permit(:title, :notes, :category_id, :tray_id, images_files: [])
end

这是我调用动作的按钮

<%= link_to 'TRANSPLANT', transplant_plant_path(@plant), class: "btn btn-raised btn-info hoverable" %>

这是我的移植页面 _transplant.html.erb

<div class="row">
  <div class="col-md-8 col-md-offset-2">
    <div class="jumbotron">
      <%= form_for(@plant, html: {class: 'form-horizontal'}) do |f| %>
          <% if @plant.errors.any? %>
              <div id="error_explanation">
                <h2><%= pluralize(@plant.errors.count, "error") %> prohibited this grow from being saved:</h2>

                <ul>
                  <% @plant.errors.full_messages.each do |message| %>
                      <li><%= message %></li>
                  <% end %>
                </ul>
              </div>
          <% end %>
          <%= f.label 'NAME' %>
          <%= f.hidden_field :tray_id, value: params[:tray_id] %>
          <% if params[:tray_id].nil? %>
              <%= f.collection_select(:tray_id, Tray.all, :id, :title) %></br></br>
          <% end %>

          <%= f.submit class: 'btn btn-raised hoverable btn-success' %>
      <% end %>
    </div>
  </div>
</div>

修改 实施后移植路线 并将我的链接代码更改为

<%= link_to "TRANSPLANT", transplant_plant_path(@plant, tray_id: @plant.tray_id), method: "post", class: "btn btn-raised hoverable" %>

我仍然得到同样的错误。它指向我控制器中的plant_params代码。

这些是正在传递的参数:

{"_method"=>"post",
 "authenticity_token"=>"fhSKt2DpgTwt1J4HsoBqYFSs0B9+pgSvDDxrS/u6yo4c3gvSxYlrrFDmhbPXq+cMho/eTHY+194WZ8zpcb1txA==",
 "id"=>"1",
 "format"=>"1"}

我只是想更新:tray_id

我整天都在这一天,任何人都可以帮助解决我得到的错误吗?

3 个答案:

答案 0 :(得分:2)

您应该提供移植操作和视图的代码。根据您提供的内容,您似乎正在尝试构建一个链接,该链接会在单击时更改工厂托盘。如果是这种情况,移植应该是POST路线而不是GET。此外,您可能希望在帖子链接中提供tray_id,如下所示:

<%= link_to "TRANSPLANT", transplant_plant_path(@plant, tray_id: {{your id}}), method: "post", class: "..." %>

然后你可以通过params[:tray_id]获取托盘中的tray_id并重新关联你的植物和托盘

答案 1 :(得分:1)

基本上我试图做的事情并不容易,我的方法需要改变。我只是在我的视图中渲染移植形式,现在它工作正常。再次感谢:)

答案 2 :(得分:0)

检查您刚刚定义std::vector<Box*> Boxes; struct Box { int number; Box() { Boxes.push_back(this); }; ~Box() { Boxes.erase(std::remove(Boxes.begin(), Boxes.end(), this), Boxes.end()); } }; 路线的路线,并在表单提交后您的请求为get