I have a model 'Article' and a model 'Ratings' nested within articles.
/articles/123/ratings
I want to change the routing of the f.submit in ratings/_form.html.erb
now it is so, that after pressing submit, my application routs to
/ratings/111
but I want to route it to
/article/123
How can I change the routing in a form_for f.submit button.
I have found here something like this:
<% form_for :thing, :url =>
url_for(:action => "update", :id => #thing) do |f| %>
But this do not work for my rails 3.2. Thanks for your help,
:url - The URL the form is submitted to. It takes the same fields you pass to url_for or link_to. In particular you may pass here a named route directly as well. Defaults to the current action.
<% form_for :thing, :url => {:action => "update", :id => #thing} do |f| %>
you can also pass it the path_name by using the helper. so you can also do something like
:url => update_article_path(#article)
Try form_for (:thing, url:{:controller=>'thing', :action=>'update'}, html:{method:'put'}).
Related
I'm having a hard time with the Paperclip gem. I'm trying to upload a picture as an avatar for my user. Everytime I upload, it will get the file and store it in the User DB table. I'm trying to get my simple form to go through action='/users/<%=session[:user_id]%>', which then will go through user#update (which then will update my users picture), but it just gives me an error:
`undefined local variable or method `update_users_path' for '
form_for #user, :url => update_users_path, :method => patch, :html => {:multipart => true} do |f| %>
<%= f.label :avatar %>
<%= f.file_field :avatar %>
<div class="form-action">
<%= f.submit :submit %>
</div>
<% end %>
I tried to do it without Simple Form, but I need multipart to be true in order to pass back the file.
The path to update a User is not update_users_path, it is user_path(#user) with :method =>:patch or :method => :put. RESTful routing connects PUT /users/:id to the #update action. Take a look at the Rails Routing guide for more details.
You should be able to build the form properly with just:
form_for #user, :method => :patch, :html => {:multipart => true}
The URL will be inferred from the method and #user, or if you do want to include it, use :url => user_path(#user). In fact, because the #user already exists, Rails will infer :method for you too.
Since you are using form_for (and presumably a recent version of Rails), you also don't need to explicitly add multipart. As the Form Helpers guide indicates, Rails will figure that out from the presence of f.file_field and include it for you.
The error is that update_users_path is not a valid route.
--
The route will be user_path(#user) with method: :patch (if you're using resources :users in the routes)
If you have #user populated properly, you shouldn't have issue by just passing it directly to the form_for as follows:
#app/views/users/edit.html.erb
<%= form_for #user, multipart: true do |f| %>
I am trying to define the action "savenew" in admin/photographers controller.
I have tried this:
<%= simple_form_for(:photographer_savenew, :action => 'savenew', :id => params[:id], :multipart => true ) do |f| %>
But the action in the form is still: /admin/photographers
When it should be: /admin/photographers/savenew
Is there a reason you're not using REST for this? It would make your life a lot easier and requires much less code. If you're set on using this custom action, you will need to specify the url and probably the method:
<%= simple_form_for #photographer, :url => savenew_photographers_path, :method => :post ... # etc
Hi
I have asked a question similar to this before but never got it resolved. So I am trying again.
This seems like it should be so simple. I am not using Rails 3 yet BTW.
All I want to do is have a drop down menu and when a person chooses that location and presses "go" they go to that page.
<% form_tag installation_path([:id]), :url => { :action => "show" }, :method => :get do %>
<%= select_tag :id, options_from_collection_for_select(Installation.find(:all), :id, :name) %>
<%= submit_tag 'Go' %>
<% end %>
This becomes the issue: http://localhost:3000/installations/id?id=1&commit=Create. It can't find the :id. I just don't know how to route this correctly. It seems like this shouldn't be that difficult.
Any help would be great. Thanks.
I think there might be a problem with your form_tag. It seems you're defining the path twice.
Both
installation_path([:id])
and
:url => { :action => "show" }
are used to generate the path but I don't think you should be using both. Just go with
installation_path([:id])
or
:url => { :controller => "installations", :action => "show", :id => id }
You need to create and use a new "show" route that is not based on the installation id (and doesn't collide with Rails resource routes), and continue to send the installation id into the controller's show action as part of the params object.
In routes.rb,
get 'show_installation', to: 'installations#show'
In your view,
<% form_tag show_installation_path, :method => :get %>
...
I have a form which is always submitting the form to the "update" function in the controller, and I have created this form using "remote_form_for" tag. In this form I have objects from different tables, and from this form I want to submit entire form data to another function(not to the "update" function) via AJAX request.
I have tried many methods including using submit tag with action
<% remote_form_for #employee, :url => organization_employee_path(#organization, #employee), :method => :put do |employee_form| %>
// form objects and other functionalities
....
....
// views inside the forms
<div id="employee_header_div">
<%= render :partial => "employee_header", :locals => {:employee => #employee} %>
</div>
...
...
<%= submit_tag "page_level_validation", :id => "page_level_validation" , :action=>"validate"%>
<% end %>
But the Ajax request always calling the same "update" function.
It would be very helpful, if anyone helps to resolve this issue.
You can't set the submit to point to a different place than the main form has specified (unless you want to use the HTML5 formaction attribute and deal with the browser compatibility consequences).
However, what you could do is create a new action in your controller which deals with the situation.
e.g..
<% remote_form_for #employee, :url => organization_employee_validate_path(#organization, #employee), :method => :put do |employee_form| %>
in your controller
def validate
#do something loosely based around the update method
end
not forgetting to add the appropriate routes.
Try this:
<% form_for #employee, :remote => true, :url => organization_employee_path(#organization, #employee), :method => :put do |employee_form| %>
I am having a problem with the form_for method in Rails. It is behaving strangely.
I have a route with a path prefix, something like:
map.resources :beers, :path_prefix => '/:brewery'
And I have a form like this (#beer.brewery is a string, just the name of the brewery):
<% form_for #beer, :url => { :brewery => #beer.brewery } do |form|
--some fields
<% end %>
It will set the action of the form to this for a new record.
/brewery_name/beers/new
and this for an existing record.
/brewery_name/beers/1/edit
Anyone knows why this happens or how to fix it?
--edit--
Right now I am solving this like this (for a new record):
<% form_for #beer, :url => beers_path(#beer.brewery) do |form| %>
and (for an edited record)
<% form_for #beer, :url => beer_path(#beer.brewery, #beer) do |form| %>
But I want to do it the same way for new and edit, if it is possible.
Cheers,
Thijs.
I'm guessing you want this to go to a brewery's beer. In that case:
<% form_for [#beer.brewery, #beer] do |f| %>
-- some fields
<% end %>
By providing an Array as the first argument to form_for it will generate a nested resource.