How to define action with simple form for? - ruby-on-rails

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

Related

How go to controller action correctly, using haml form?

When I go to set_answer action(localhost:3000/staff/set_answer/) from answer action(localhost:3000/staff/answer/) , I get this url:
localhost:3000/staff/answer/set_answer but this is wrong URL . How get this url:
localhost:3000/staff/set_answer
this is how i set action where should go parameters
%form{ :controller => "staffs", :action => "setanswer", :method => "post"}
Assuming you're using Rails, you should implement a form using the form_tag helper method.
Try this:
= form_tag staff_setanswer_path do
// ...
= submit_tag
or this:
= form_tag :controller => :staff, :action => :setanswer do
// ...
= submit_tag
And if you want to stay with a Haml form, try this:
%form{:action => staff_setanswer_path, :method => :post}
I'm guessing routes here. You might adapt the _path helper here and there.

Rails: Change Form Method on Condition

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

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

How to set the action with form_for?

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

How can I change the method used in Formtastic?

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.

Resources