开发者

Rails 3 Auto Select from Collect_select

I have the following associations:

Town -> has_many :outlets
User -> belongs_to :town
Outlet -> belongs_to :town, has_many :problems
Problem -> belongs_to :outlet

In the Outlets Page, I want to be able to click a button that will take me to the New Problem page. In the 'New Problem Page' I have the following collection_select:

f.collection_select :outlet_id, Outlet.all, :id, :na开发者_如何学JAVAme

However, if I clicked the button that took me here from the Outlet Page, I want the correct outlet to already be selected and greyed out so the user can't change it.

I was thinking I could possibly do this with a custom route, that will accept the :outlet_id as a param, but I wasn't sure how to do this, or even if this is the best option.

Any help is greatly appreciated.


In your controller, you have something like

@problem = Problem.new

try

@problem = Problem.new({ outlet_id: params[:outlet_id] })

And the form helpers should take care of it.

You could also try to do it as a nested RESTful resource in which case the url would look something like /outlets/5/problems/new and rails would automatically set params[:outlet_id] to 5..

Your route for this would look like:

resources :outlets do
  resources :problems    
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜