将参数传递给新的控制器操作

时间:2018-04-02 02:02:33

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

Rails 4.21
Ruby 2.5

我有一个使用标准MVC结构的组织模型。

show动作,显示组织的视图(姓名,联系信息等)。

我添加了一个OrganizationNote MVC结构。

我想做的是,在组织展示视图中,添加一个"添加注释"按钮,类似于:

link_to 'Add Note', new_organization_note_path, :class => "btn btn-primary btn-lg"

但我需要将组织ID传递给New OrganizationNote。现在,当我尝试时,没有参数与调用一起传递:

link_to 'Add Note', new_organization_note_path(@organization.id), :class => "btn btn-primary btn-lg"

我没有看到任何参数传递。

我如何实现这一目标,是否需要创建任何特殊路线?

编辑:

我在routes.rb中添加了以下内容:

resources :organizations do
  resources :organization_notes
end

产生了以下路线:

organization_organization_notes GET /organizations/:organization_id/organization_notes(.:format) organization_notes#index
POST /organizations/:organization_id/organization_notes(.:format) organization_notes#create
new_organization_organization_note GET /organizations/:organization_id/organization_notes/new(.:format) organization_notes#new
edit_organization_organization_note GET /organizations/:organization_id/organization_notes/:id/edit(.:format) organization_notes#edit
organization_organization_note GET /organizations/:organization_id/organization_notes/:id(.:format) organization_notes#show
PATCH /organizations/:organization_id/organization_notes/:id(.:format) organization_notes#update
PUT /organizations/:organization_id/organization_notes/:id(.:format) organization_notes#update
DELETE /organizations/:organization_id/organization_notes/:id(.:format) organization_notes#destroy

我将链接修改为:

link_to 'Add Note', new_organization_organization_note_path(:organization_id => @organization.id), class: 'btn btn-primary btn-lg'

在organization_notes_controller.rb中,我有:

def new
  @organization_note = OrganizationNote.new
  @organization = Organization.find(params[:organization_id])
end

当我点击Add Note,按钮时,我收到以下消息:

Incomplete response received from application

从日志文件中:

Started GET "/organizations/176/organization_notes/new" for xx.xxx.xxx.xxx at 2018-04-02 03:24:32 +0000
Processing by OrganizationNotesController#new as HTML
  Parameters: {"organization_id"=>"176"}
  [1m[35mUser Load (0.1ms)[0m  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1  ORDER BY `users`.`id` ASC LIMIT 1
  [1m[36mOrganization Load (0.1ms)[0m  [1mSELECT  `organizations`.* FROM `organizations` WHERE `organizations`.`id` = 176 LIMIT 1[0m
  Rendered organization_notes/_form.html.erb (34.7ms)
  Rendered organization_notes/new.html.erb within layouts/application (35.6ms)
Completed 500 Internal Server Error in 44ms (ActiveRecord: 0.3ms)

2 个答案:

答案 0 :(得分:1)

要使OrganizationNote使用相对于Organization的网址,您需要使用nested resources。这将允许您指定要将Note应用于哪个组织的参数,例如

resources :organizations do
    resources :organization_notes
end

您可以使用rails routes确认路径现在包含参数。

(如果你只想要这个相对路线来创建Notes,你也可以只使用一个自定义路由而不是resources函数。)

答案 1 :(得分:0)

您可以尝试以下解决方案。

# routes.rb
resources :organizations do
  resources :organization_notes
end

# new method
def new
  @organization = Organization.find(params[:organization_id])
  @organization_note = @organization.organization_notes.build
end

# views
link_to 'Add Note', new_organization_organization_note_path(:organization_id => @organization.id), class: 'btn btn-primary btn-lg'