Reputation: 7011
Say I've got a standard new
method in a controller:
def new
@doc = Doc.new
respond_to do |format|
format.html
format.json { render json: @doc }
end
end
How would I facilitate passing an argument through it, i.e.:
def new(i)
...
end
To permit me to write something like this in the view:
<%= link_to(e.name, new_doc_path(e.id)) %>
Cheers!
Upvotes: 1
Views: 5612
Reputation: 43318
Rails doesn't work like that. If you want to pass anything to the controller you have to use the params
hash. In your example:
View:
<%= link_to(e.name, new_doc_with_parameter_path(e.id)) %>
Controller:
def new
id = params[:id]
# do something with `id`
end
For this to work you have to change your routes so that you can pass an id
via the URL to your new action:
get "/docs/new/:id" => "docs#new", :as => :new_doc_with_parameter
Although the above should work, in your case it may be better to have a look at Nested Resources.
Upvotes: 7