How to set the action with form_for? - ruby-on-rails

I created a new page on an existing controller.
I added 2 action methods on the controller: prompt_user and process_feedback.
So I get to the page via
redirect_to :controller => :users, :action => :prompt_user
And the form_for code looks like
<% form_for :user, #user do |f| %>
Which generates the following html
<form action="users/prompt_user" method="post">
Notice the action is prompt_user, where as I want to set it to process_feedback. I thought I could change the action with a button
<%= submit_tag "Process feedback" %>
But that didn't work.
So my question is how can I change the action to process_feedback?
Also, as you can probably tell, I'm very new to rails, so if I'm doing something especially obtuse, I'd love to find out what it is.

This is from memory, but I think you can do something like this:
form_for :user, #user, :url => { :action => :prompt_user } do |f|

Alternatively the same can be reached using form_tag with the syntax:
See my answer on this question: here https://stackoverflow.com/a/37145293/1248725

Related

Rails change routing of submit in form_for

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'}).

Form submission in rails 3

I decided to start a little project in rails 3 and I am a little bit stuck on a form... Where can I specified the f.submit action should go to a special controller / action ?
The code in the form is:
<%= form_for #user, :url => { :action => "login" } do |f| %>
<div class="field">
<%= f.text_field :email %><br />
<%= f.text_field :password %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
User is defined as #user = User.new in "index" method of "home_controller".
but I have the error:
No route matches {:controller=>"home", :action=>"login"}
as soon as I run http://0.0.0.0:3000
I am very sorry for this newbee question but I cannot find the routing details (I worked a little bit with rails a couple of years ago but...)
Thanks,
Luc
You don't need to specify any action for f.sumbit.
First of all, you need to make sure you put
resources :users
(for example)
in your routes.rb
then if you want to create a user
put
def new
#user = User.new
end
in your users_controller so you have a page to create new user
or you can put #user=User.new anywhere you like, remember to set
the route correctly
then
def create
#user = User.new(params[:id])
if #user.save
sign_in #user
redirect_to #user
else
render 'new'
end
end
is the part that does real work after you hit on submit
the actual part that connect your form with everything else is this line
<% form_for #user do |f| %>
you can change user to other object, and you can also edit form using update action in a controller.
Hope you got the idea
Whenever you use REST objects, the mere:
form_for #article
is enough for the form to find the proper path.
Otherwise, you can use helpers this way:
form_tag(:controller => "people", :action => "search", :method => "get", :class => "nifty_form")
More info here: http://edgeguides.rubyonrails.org/form_helpers.html

Select_tag submitted needs to redirect to show page with :id?

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 %>
...

Clearing controllers in Rails 2

I made a Wysiwyg module where a user can create custom text areas for different sections of their website.
I do this by checking in the controllers if they have created one for this particular section yet. If they have, it redirects them :
def new
if Wysiwyg.find_by_name(params[:name]) != nil
redirect_to edit_admin_wysiwyg_path(Wysiwyg.find_by_name(params[:name]))
else
#wysiwyg = Wysiwyg.new(:name => params[:name])
end
end
The trouble is is Rails still believes its a 'new' even though I have redirected the user to edit. How can I 'clear' the controller's and make it really sincerely believe it is actually an 'edit' ?
Thanks!
Ah my problem was in my form instantiation
Old and Evil:
<% form_for(#wysiwyg, :url => admin_wysiwygs_path, :html => { :method => :post}) do |f| %>
Correct:
<% form_for #wysiwyg, :url => admin_wysiwyg_path do |f| %>

Problem with form_for Helper

I have the following form_for declaration in a Rails site I am building:
form_for(form_question, :url => { :controller => "form_questions", :action => "edit", :id => form_question.id }) do |f|
but the site renders;
<form action="/form_questions/1/edit">
why is it putting the '/1/' before the "edit" in the action url?
Simple Answer. RESTful routes.
Basically you have defined resources :form_questions in config/routes.rb and that is transforming, automagically, your URL to make it RESTful.
I would recommend using the RESTful helpers provided to you, like:
<% form_for(#form_question) do %>
<%= f.text_field :question %>
...
<% end %>
Which will generate a URL to either create or update depending on if the #form_question response to new_record? is true or false respectively. It'll also do other things, like give the form tag a different id attribute based also off what new_record? returns.

Resources