Take input in the view and pass it to the contoller - ruby-on-rails

The following code in not rendering even the buttons:
<% form_tag :controller=> :create_new, :action=>:input do %>
<%=text_field_tag :my_input%>
<%=submit_tag "Send input"%>
<%end%>
the controller create_new has the following method:
def input
#my_input=params[:my_input]
end
the routes.rb has::
resources :create_new do
post :input, :on=>:collection

Use <%= form_tag instead of <% form_tag.

For rails 3 we have to use '=' symbol for form_tag and form_for. so change your code as follows..
<%=form_tag :controller=> :create_new, :action=>:input do %>
<%=text_field_tag :my_input%>
<%=submit_tag "Send input"%>
<%end%>

<%= form_tag input_create_new_path, :html_options => {:method => :post} do |f| %>
<%= f.text_field :my_input%>
<%= f.submit "Send input"%>
<%end%>

Related

form_tag with same params as current rails

How do I create a form_tag with the same current params but an additional param?
<%= form_tag(params.merge( :controller => "integrations", :action => "show" ), method: :get) do %>
<%= text_field_tag :params, params[:q] %>
<%= submit_tag "Search", class: "btn btn-default"%>
<% end %>
where my url looks like:
http://localhost:3000/integrations/64?utf8=%E2%9C%93&q%5Bbio_or_topics_or_title_or_company_cont_any%5D=mcmaster&q%5Btitle_cont%5D=&q%5Bcompany_cont%5D=&q%5Blocation_cont_any%5D=&sort_by=twitter_followers&q%5Bfacebook_username_not_eq%5D=&q%5Btwitter_username_not_eq%5D=&q%5Blinkedin_username_not_eq%5D=&q%5Blocation_not_eq%5D=&commit=Search
Thanks!

How to implement a destroy confirmation form with simple_form?

I'm using Ruby on Rails 4 with simple_form. On my delete page, I would like to display a form confirming the user's decision to destroy the resource in question. Here is the code I am using:
<h2>Delete Page</h2>
<p>Are you sure you want to delete <%= #journalEntry.title %></p>
<%= simple_form_for(#journalEntry, :action => 'destroy') do |f| %>
<%= f.button :submit %>
<% end %>
However, this is getting processed by the update action instead (my server console shows that it is being sent as a PATCH request).
I also tried amending that code to the following, but with the same result:
<%= simple_form_for(#journalEntry, :url => {:action => 'destroy', :id => #journalEntry.id}) do |f| %>
<%= f.button :submit %>
<% end %>
Add :method => :delete option with simple_form_for :
<%= simple_form_for(#journalEntry, :method => :delete) %>
<%= f.button :submit %>
<% end %>

I want to take input in the view and pass it to the controller

The following code in not rendering even the buttons:
<% form_tag :controller=> :create_new, :action=>:input do %>
<%=text_field_tag :my_input%>
<%=submit_tag "Send input"%>
<%end%>
the controller create_new has the following method
def input
#my_input=params[:my_input]
end
the routes.rb has:
resources :create_new do post :input, :on=>:collection
The = was missing.
<%= form_tag :controller=> :create_new, :action=>:input do %>
<%= text_field_tag :my_input%>
<%=submit_tag "Send input"%>
<%end%>
<% form_tag :controller=> :create_new, :action=>:input do %>
It has a missing = , check this
<%= form_tag :controller=> :create_new, :action=>:input do %>
As the form tag is a helper it actually needs to be displayed in the view. the <% just allows code to run, does not actually display the result of the code.
Try using
<%= form_tag :controller=> :create_new, :action=>:input do %>
this includes the = which will actually display the result of the code run.
Hope that helps.

ruby on rails ignores my form_tag

I have written in my index.html.erb the following lines
<% form_tag illnesses_path, :method =>'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
expecting that it will write a form element but rails completrly neglected those line.
when I wrote
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
the items apeared (but no form element).
Why was the form element neglected
Change
<% form_tag illnesses_path, :method =>'get' do %>
to
<%= form_tag illnesses_path, :method =>'get' do %>.

Adding an ID or Class in Ruby on Rails?

I've got the following code for a search form, but how would I add an ID or a class to the submit button?
<% form_tag '/wine/search/', :method => 'get' do %>
<%= label_tag "Search" %>
<%= text_field_tag :search_string, params[:search_string] %>
<%= submit_tag "Go" %>
<% end %>
Thanks
submit_tag "Go", :class => "some_class"
submit_tag "Go", :id=> "some_id"
From the Rails API FormTagHelper

Resources