I was wondering how can I specify the method to send the data from a Formtastic form.
It usually uses POST, but I'd like it to use GET in some cases (i.e. for a search form).
Is it possible/easy?
You can specify the :method in the semantic_form_for tag, for a normal form_for tag it would look something like this:
<% form_for :post, #post, :url => post_path(#post), :html => { :method => :get } do |f| %>
I'm pretty sure it works the same for Formtastic forms.
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 developing a rails app and I have a form where I want to change the method (:put vs :post) based upon the condition: params[id].to_s.present?. Here is my attempt, but no matter what I do, I get various errors:
<%= form_tag( country_pend_create_path(), "#{params[:id].to_s.present? ? ':method => :put' : ':method => :post'}", :class => 'form-horizontal') do %>
Know I know that form_for() preforms this purpose automatically, but I cannot and do not want to use form_for() for this form. Any ideas on how to get this to work?
You can simply write as:
<%= form_tag( country_pend_create_path(), :method => params[:id].present? ? :put : :post, :class => 'form-horizontal') do %>
Try using a lambda:
<%= form_tag( country_pend_create_path(), :method => lambda { params[:id].present? ? :put : :post }, :class => 'form-horizontal') do %>
(I removed the to_s since I don't think it changes anything, add it back if it's important.)
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'}).
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
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| %>