ruby-on-rails - 参数缺失或值为空 : plant

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

我在我的 Controller 中添加了一个名为移植的自定义操作。我只是想呈现一个下拉表单以根据“tray_id”选择要定位的位置

我的路线是这样的:

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

我的 Controller 看起来像这样:

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

这是调用 Action 的按钮

<%= 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" %>

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

这些是正在传递的参数:

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

我只是想更新 :tray_id

我整天都在做这个,有人可以帮助解决我遇到的错误吗?

最佳答案

您可能应该为移植操作和 View 提供代码。根据您提供的内容,您似乎正在尝试建立一个链接,单击该链接会更改植物托盘。如果是这样的话,移植应该是 POST 路由而不是 GET。此外,您可能希望在您的帖子链接中提供 tray_id,如下所示:

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

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

关于ruby-on-rails - 参数缺失或值为空 : plant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37199386/

相关文章:

ruby-on-rails - after_create :foo vs after_commit :bar, :on => :创建

ruby-on-rails - Rails 中带有动态内容的侧边栏的设计模式

html - 如何将 View 中的脚本移动到 Ruby On Rails 中的单独 js 文件?

ruby-on-rails - 如何从 Controller 的Class方法访问路由路径? (路轨)

ruby-on-rails - Rails respond_with & Rspec Controller : Testing unsuccessful update

ruby-on-rails - 忘记密码设计 gem API

ruby - 使用 rake 复制保留目录结构的文件

ruby - 为什么有些 Ruby 项目的文件只有 'require' 语句

ruby-on-rails - Ruby/Rails - 在新窗口中从 Controller 打开 URL

javascript - Rails 表单和对不同 Controller 的自定义 Ajax 调用