简体   繁体   English

Rails-将参数从一个视图传递到另一个控制器

[英]Rails - Passing a parameter from one view to a different controller

Fairly simple question, and I've seen lots of similar ones asked in my search, but none of the solutions I've read have worked so far. 这个问题很简单,我在搜索中看到很多类似的问题,但是到目前为止,我所读过的解决方案都没有奏效。 Not sure where I'm going wrong. 不知道我要去哪里错了。

In my application I have Projects and Verifications. 在我的应用程序中,我有项目和验证。 When viewing a particular Project, I need to be able to have a link to "Verify Project", which will send the user to a new Verification form. 在查看特定项目时,我需要能够链接到“验证项目”,该链接会将用户发送到新的“验证”表单。 They will enter a variety of values, but I do not want them to have to select the Project that this Verification belongs to by hand -- I want to be able to pass the project_id directly alongside the values the user enters in the Verification form. 他们将输入各种值,但是我不希望他们必须手动选择此验证所属的项目-我希望能够直接将project_id与用户在“验证”表单中输入的值一起传递。

On my project view I have: 在我的项目视图中,我有:

<%= link_to "Verify Project", new_verification_path(:project_id => @project.id ) %>

and then in the verifications controller: 然后在验证控制器中:

def new
    @verification = Verification.new(params[:verification])
    @project = params[:project_id]
end

  def create
    @verification = Verification.new(params[:verification])
    @verification.project = @project
end

but this yields this error: 但这会产生此错误:

Validation failed: Project does not exist, Project does not exist

How can I create my Verification with :project_id being grabbed from the previous page? 如何通过上一页中的:project_id来创建我的验证?

EDIT 编辑

From the Rails log: (clicking Verify Project) 在Rails日志中:(单击“验证项目”)

Started GET "/verifications/new?project_id=4" for 127.0.0.1 at 2013-09-10 04:02:56 +0200 Processing by VerificationsController#new as HTML Parameters: {"project_id"=>"4"} Rendered verifications/_form.html.erb (91.3ms) Rendered verifications/new.html.erb within layouts/application (97.5ms) 在2013-09-10 04:02:56 +0200 VerificationsController#new作为HTML参数开始针对127.0.0.1的GET“ / verifications / new?project_id = 4”的HTML参数:{“ project_id” =>“ 4”}呈现的验证/_form.html.erb(91.3ms)在布局/应用程序中呈现的验证/new.html.erb(97.5ms)
Rendered layouts/_shim.html.erb (0.3ms) Account Load (0.2ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."remember_token" = 'NuxEYIjeCYyFklN7EyHTDQ' LIMIT 1 Rendered layouts/_header.html.erb (38.6ms) Rendered layouts/_footer.html.erb (0.3ms) Completed 200 OK in 381ms (Views: 368.4ms | ActiveRecord: 2.7ms) 呈现的布局/_shim.html.erb(0.3毫秒)帐户加载(0.2毫秒)选择“账户”。*从“账户”中的“账户”。“ remember_token” ='NuxEYIjeCYyFklN7EyHTDQ'LIMIT 1呈现的布局//header.html.erb (38.6ms)渲染的layouts / _footer.html.erb(0.3ms)在381ms内完成200 OK(查看:368.4ms | ActiveRecord:2.7ms)

and then Create 然后创建

Started POST "/verifications" for 127.0.0.1 at 2013-09-10 04:03:04 +0200 Processing by VerificationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"sqUBXqA6y5oKCW1DFAi3sv8rQzm+tKjOYDdc/lvUS+c=", "verification"=>{"verifier_name"=>"test", "checked_on(1i)"=>"2013", "checked_on(2i)"=>"9", "checked_on(3i)"=>"10", "notes"=>"test"}, "commit"=>"Create Verification"} Account Load (0.2ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."remember_token" = 'NuxEYIjeCYyFklN7EyHTDQ' LIMIT 1 (0.0ms) begin transaction 在2013-09-10 04:03:04 +0200通过VerificationsController#create作为HTML参数开始针对127.0.0.1的POST“ /验证”:{“ utf8” =>“✓”,“ authenticity_token” =>“ sqUBXqA6y5oKCW1DFAi3sv8rQzm + tKjOYDdc / lvUS + c =“,” verification“ => {” verifier_name“ =>” test“,” checked_on(1i)“ =>” 2013“,” checked_on(2i)“ =>” 9“,” checked_on( 3i)“ =>” 10“,”笔记“ =>” test“},”提交“ =>”创建验证“}帐户加载(0.2毫秒)选择“帐户”。*从“帐户”中选择“帐户”。 “ remember_token” ='NuxEYIjeCYyFklN7EyHTDQ'LIMIT 1(0.0ms)开始交易
(0.1ms) rollback transaction Completed 422 Unprocessable Entity in 18ms (0.1毫秒)回滚事务在18毫秒内完成了422个不可处理实体

ActiveRecord::RecordInvalid (Validation failed: Project does not exist, Project does not exist): ActiveRecord :: RecordInvalid(验证失败:项目不存在,项目不存在):
app/controllers/verifications_controller.rb:55:in `create' app / controllers / verifications_controller.rb:55:在'create'中

EDIT 2 编辑2

Here's the form for the Create action: 这是“创建”操作的表单:

<%= form_for(@verification) do |f| %>
  <% if @verification.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@verification.errors.count, "error") %> prohibited this verification from being saved:</h2>

      <ul>
      <% @verification.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<div class="row">
  <div class="span6 offset3">
  <div class="field">
    <%= f.label :verifier_name %><br />
    <%= f.text_field :verifier_name %>
  </div>
  <div class="field">
    <%= f.label :checked_on %><br />
    <%= f.date_select :checked_on %>
  </div>

  <div class="field">
    <%= f.label :notes %><br />
    <%= f.text_area :notes %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div></div></div>

<% end %>

Your new controller action is a bit off. 您的new控制器操作有些偏离。 You're assigning an actual id to the @project instance variable, rather that the Project object that corresponds to that id . 您正在为@project实例变量分配实际的id ,而不是与该id对应的Project对象。 Subsequently, you're passing an invalid id from your new view to your create action. 随后,您会将无效的id new视图传递到create动作。

Fix things by properly assigning the @project instance variable in your new action: 通过在new操作中正确分配@project实例变量来解决问题:

def new
    @verification = Verification.new(params[:verification])
    @project = Project.find(params[:project_id])
end

Then, you'll need to do a proper lookup of the a Project object in your create action: 然后,您需要在create动作中对Project对象进行适当的查找:

def create
    @verification = Verification.new(params[:verification])
    @verification.project = Project.find(params[:project_id])
    @verification.save
end

Finally, add the project_id as parameter in your form via the hidden_field form helper tag: 最后,通过hidden_field表单帮助标签将project_id添加为表单中的参数:

# in your `create` form
<%= f.hidden_field :project_id, :value => @project.id %>

You need to be sure that your project id is passed back to your create method. 您需要确保将项目ID传递回create方法。 You can look up the project as in zeantsol's answer, or you can just pass the project ID. 您可以按照zeantsol的答案查找项目,也可以只传递项目ID。 But you need to make sure that the form you create in your "new" view passes the project id back to your create method (perhaps in a hidden field) 但是,您需要确保在“新”视图中创建的表单将项目ID传递回创建方法(可能在隐藏字段中)

See this post to see an example of using a hidden field in your view: Rails hidden field undefined method 'merge' error 请参阅这篇文章,以查看在视图中使用隐藏字段的示例: Rails隐藏字段未定义方法“合并”错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM