重定向以显示另一个控制器中的操作

时间:2022-12-01 22:48:20

After creating a Person associated with a specific Account, how do I redirect back to the Account page?

在创建与特定帐户相关联的人之后,如何重定向回帐户页面?

The account_id is passed to the CREATE Person action via a URL parameter as follows:

account_id通过URL参数传递给CREATE Person操作:

http://localhost:3000/people/new?account_id=1

Here is the code:

这是代码:

<h2>Account: 
    <%= Account.find_by_id(params[:account_id]).organizations.
        primary.first.name %>     
</h2>

<%= form_for @person do |f| %>

    <%= f.hidden_field :account_id, :value => params[:account_id] %><br />  
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name %><br />
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %><br />
    <%= f.label :email1 %><br />
    <%= f.text_field :email1 %><br />
    <%= f.label :home_phone %><br />
    <%= f.text_field :home_phone %><br />
    <%= f.submit "Add person" %>

<% end %>

class PeopleController < ApplicationController

    def new
        @person = Person.new
    end

    def create
        @person = Person.new(params[:person])
        if @person.save
            flash[:success] = "Person added successfully"
            redirect_to account_path(params[:account_id])
        else
            render 'new'
        end
    end
end

When I submit the above form I get the following error message:

当我提交上述表格时,我收到以下错误信息:

Routing Error

No route matches {:action=>"destroy", :controller=>"accounts"}

Why is the redirect_to routing to the DESTROY action? I want to redirect via the SHOW action. Any help will be greatly appreciated.

为什么重定向到销毁操作的路由?我想通过SHOW action重定向。如有任何帮助,我们将不胜感激。

2 个解决方案

#1


7  

params[:account_id] exists in the form, but when you pass it to create you're sending it along in the person hash, so you'd access it via params[:person][:account_id]

params[:account_id]存在于表单中,但是当您传递它来创建时,您将它发送到person散列中,因此您将通过params[:person][:account_id]访问它

params[:account_id] is nil, hence the bad route. To be honest, I'm not sure why, but resource_path(nil) ends up routing to destroy instead of show. In either case, it's a broken route without an id parameter.

params[:account_id]是nil,因此路由错误。老实说,我不知道为什么,但resource_path(nil)最终以破坏而不是显示结束路由。无论哪种情况,它都是一个没有id参数的中断路由。

# so you *could* change it to:
redirect_to account_path(params[:person][:account_id])

# or simpler:
redirect_to account_path(@person.account_id)

# but what you probably *should* change it to is:
redirect_to @person.account

Rails will inherently understand this last option, ascertaining the path from the class of the record, and getting the id from #to_param

Rails将天生理解这最后一个选项,确定来自记录类的路径,并从#to_param获取id

#2


1  

I would not be pass this through using a hidden_field. Instead, use nested resources:

我不会使用hidden_field传递这个。相反,使用嵌套的资源:

resources :account do
  resources :people
end

Then have an account object for the form:

然后为表单设置一个account对象:

<%= form_for [@account, @person] do |f| %>
  ...
<% end %>

This @account object should be set up in the action that renders the form with a line like this:

这个@account对象应该在呈现表单的动作中设置为:

@acccount = Account.find(params[:account_id])

Then when the form is submitted you'll have params[:account_id] in that action without the ugly hidden_field hack to get it there.

然后,当表单被提交时,您将在该操作中使用params[:account_id],而无需使用难看的hidden_field hack来实现它。

Yippee!

好啊! !

#1


7  

params[:account_id] exists in the form, but when you pass it to create you're sending it along in the person hash, so you'd access it via params[:person][:account_id]

params[:account_id]存在于表单中,但是当您传递它来创建时,您将它发送到person散列中,因此您将通过params[:person][:account_id]访问它

params[:account_id] is nil, hence the bad route. To be honest, I'm not sure why, but resource_path(nil) ends up routing to destroy instead of show. In either case, it's a broken route without an id parameter.

params[:account_id]是nil,因此路由错误。老实说,我不知道为什么,但resource_path(nil)最终以破坏而不是显示结束路由。无论哪种情况,它都是一个没有id参数的中断路由。

# so you *could* change it to:
redirect_to account_path(params[:person][:account_id])

# or simpler:
redirect_to account_path(@person.account_id)

# but what you probably *should* change it to is:
redirect_to @person.account

Rails will inherently understand this last option, ascertaining the path from the class of the record, and getting the id from #to_param

Rails将天生理解这最后一个选项,确定来自记录类的路径,并从#to_param获取id

#2


1  

I would not be pass this through using a hidden_field. Instead, use nested resources:

我不会使用hidden_field传递这个。相反,使用嵌套的资源:

resources :account do
  resources :people
end

Then have an account object for the form:

然后为表单设置一个account对象:

<%= form_for [@account, @person] do |f| %>
  ...
<% end %>

This @account object should be set up in the action that renders the form with a line like this:

这个@account对象应该在呈现表单的动作中设置为:

@acccount = Account.find(params[:account_id])

Then when the form is submitted you'll have params[:account_id] in that action without the ugly hidden_field hack to get it there.

然后,当表单被提交时,您将在该操作中使用params[:account_id],而无需使用难看的hidden_field hack来实现它。

Yippee!

好啊! !