Rails: Change Form Method on Condition - ruby-on-rails

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

Related

Hidden field in rails form

I have this form in a view in my project. I need to pass the task_id to a certain controller, but the log does not seem to be receiving the parameters. I don't know what the problem is.
<%= form_for :taskid, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
<%f.hidden_field :task_id, :value => task.id%>
<td><%= f.submit "اختر مهمة لاظهار احصائياتها منفرده"%></td>
<% end %>
You are missing on = after <%. The equal sign is needed whenever you want to the result appears on the HTML, so it is used with the field tags methods or render, for instance. You should not use the equal when using a if, for example, because this is not what you want to print (well, it can be, but most likely it isn't)
<%= form_for :taskid, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
<%= f.hidden_field :task_id, :value => task.id%>
<td><%= f.submit "اختر مهمة لاظهار احصائياتها منفرده"%></td>
<% end %>
However, as #AntonGrigoriev pointed out, you should use a object if you have, like this
<%= form_for #task, :url => {:action=>"index", :controller=>"statistics"}, :html => {:class => "nifty_form", :method => "GET"} do |f| %>
or you can simply use the hidden_field_tag
<%= hidden_field_tag :task_id, task.id %>
Hi please test with following code to send hidden value in rails, I have tried and worked for one of my application :
hidden_field_tag(name, value = nil, options = {}) public
eg:
<%= hidden_field_tag(:field_name,value=#offer_status)%>

How do you add an extra query string parameter via a form submit in Ruby on Rails?

I would like to be able to add an additional query string parameter on submit that is the same as the value of the classrooms_search_textbox that the user will type. How do I do this?
<%= form_tag classrooms_path, :method => :get, :id => "classrooms_search_form" do %>
<%= text_field_tag "classrooms_search_textbox", "Find a classroom" %>
<%= submit_tag "Find", :id => "classrooms_search_button", :class => "button" %>
Do I need to add a hidden_tag (and if so, how would I go about doing this?) or can I just add to the classrooms_path somehow?
Thanks!
Since you're sending it your controller first, then you can just manipulate the params in your controller method before sending it off:
params[:classrooms_query] = params[:classrooms_search_textbox]
And then go ahead and use those params to send off to the other service. There's no need to add hidden field tags or use some fancy JS code.
<%= form_tag classrooms_path, :method => :get, :id => "classrooms_search_form" do %>
<%= text_field_tag "classrooms_search_textbox", "Find a classroom" %>
<%= hidden_field_tag "classrooms_query" %>
<%= submit_tag "Find", :id => "classrooms_search_button", :class => "button" %>
$('#classrooms_search_form').submit(function() {
$('#classrooms_query').value($(classrooms_search_textbox.value());
});
That would achieve what you want. Nonetheless, maybe it is in your interest to refactor the controller or view so that it doesn't have this kind of conflicts.

Rails 3 - button_to update same page with controller#method

In my project I have the following form_tag to select a Site
<%= form_tag({:controller => "hvacs", :action => "index"}, :method => "get") do %>
<div class="field">
<%= select :p, :site_sel, #user_sites.map{|s| [s.name, s.id]} %>
</div>
<div class="actions">
<%= submit_tag("Select site") %>
</div>
<% end %>
This form_tag updates the index page through calling its method in the controller again.
I have the following button_to
<td><%= button_to 'Select', {:controller => "hvacs", :action => "select"}, :method => "get" %></td>
I would like to achieve a similar update with this as above rather than redirect to a new page with "select_path" etc, but the above does not seem to work.
How can I achieve this? Cheers!
OK, this looked so much like my AJAX problem, I tried to make it one!
I think all you need is a simple render statement in your select action
render :index
or
render :action => 'index'
But see http://guides.rubyonrails.org/layouts_and_rendering.html#using-redirect_to for more.
The following solution worked. Apologies if I was not so clear on what I was looking for.
<%= button_to 'Select', review_hvacs_path(:h => hvac, :a => params[:a], :s => params[:s]) %>
I was trying to pass parameters with the button, while staying on the review page.

How to define action with simple form for?

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

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

Resources