I have a standard delete link, and want to add a parameter to it:
<%= link_to "Delete", item, :confirm => 'Are you sure?', :method => :delete, :foo => 1 %>
The parameter shows up in the html a tag, but does not make to the server. I get "undefined local variable or method `foo'".
Here is how I am accessing it in the controller:
def destroy
puts "params[:foo]:" + params[:foo].to_s
.
.
.
redirect_to edit_bar_path(params[:foo])
The output is params[:foo]:
<%= link_to "Delete", item_path(:id => item.id, :foo => 1), :confirm => 'Are you sure?', :method => :delete %>
I think you are looking for:
item_path(item, :foo => 1)
It should appear in your params
Related
I'm working on a html slim file for a Ruby on Rails project. I want to create a button of class btn btn-primary (I'm using bootstrap) for a controller action. Name of controller is default_responses and action is edit. So I first did:
= link_to 'Test this', :controller => 'default_responses', :action => 'edit', :id => params[:id].to_i
This would become
Test this
where 7 is the id parameter and is correct for my case. However, it is not a button at all, just an anchored tag. It also redirects me to the correct page.
Then I tried
= link_to 'Test this', :controller => 'default_responses', :action => 'edit', :id => params[:id].to_i, class: 'btn btn-primary'
which gave me
Test this
This is still not what I want as it is not a button still.
Also tried = link_to 'Test this', :controller => 'default_responses', :action => 'edit', :id => params[:id].to_i, :class=> 'btn btn-primary'
It returned Test this which is still wrong.
Any suggestions? Thanks.
Rails' link_to takes multiple hash options, in your example you're supplying a single hash option, which all get passed into the url_options section. You'll need to add the curly brackets ({}) around the first hash to tell ruby which option goes to which hash.
= link_to 'Test this', { :controller => 'default_responses', :action => 'edit', :id => params[:id].to_i }, class: 'btn btn-primary'
should work. Of course, you could also use a url helper (url_for, default_responses_path, etc) instead of the first hash.
I have the following collection select which acts as a filter in a Rails app.
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All'} %>
<% end %>
This always generates a name attribute of the select element like name="doctor[id]" which results in the browser to ?utf8=✓&doctor%5Bid%5D=1, which is not quite readable.
How can I change the name attribute to just name = "doctor" or basically just remove the brackets from it?
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
The collection_select method contains the parameters "options" and "html_options". "options" allow you to add specific information, like {:include_blank => 'All'}, but does not replace html attributes.
You have to add the name to the next hash, like this:
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All'}, {:name => 'doctor'} %>
<% end %>
Have you tried:
<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
<%= collection_select :doctor, :id, #doctors, :id, :full_name, {:include_blank => 'All', :name => 'doctor'} %>
<% end %>
I've got a form for selecting TIME. I'm using Simple Form and Jquery Timepicker. How do I remove the minute field of the form. So I have two fields for the :mondayopen parameter showing up. One is the hour and the other is the minute. How can I remove the minute.
Here's how it currently looks:
My Form
<%= simple_form_for [current_user, #store], :html => {:multipart => true, :class => 'form-horizontal'} do |f| %>
</br>
<%= f.input :mondayopen, :label => 'Monday Open Time', :input_html => { :class => 'mondayo' } %>
<%= f.button :submit, class: 'btn btn-primary', style: 'margin-left: 40px;' %><%= link_to 'Your Store', user_store_path(current_user, #store), :class => 'btn btn-inverse edit_store_button_path' %>
<% end %>
<script>
$('.mondayo').timepicker({ 'step': 15 });
$('.mondayc').timepicker({ 'step': 15 });
</script>
If you have mondayopen field as time type field SimpleForm creates time_select for you but you don't need it as you are using jquery timepicker plugin. So you can just redefine it like this:
<%= f.input :mondayopen, :as => :string, :label => 'Monday Open Time', :input_html => { :class => 'mondayo' } %>
I have this link:
<%= link_to "+1", video_votes_path( :video_id => video.id, :type => "up" ), :method => :post, :remote => true %>
but I want to turn its visual appearance to this: ⇑ with the HTML entity ⇑. How can I do this while keeping the link's functionality?
<%= link_to "⇑".html_safe, video_votes_path( :video_id => video.id, :type => "up" ), :method => :post, :remote => true %>
I was wondering about how i can do this in rails
because i want something like
<a href="requests/13#new">Comment!<a>
anyone knows it?
greetings
<%= link_to "Comment!", url_for(:controller => "requests", :action => "show", :id => 13, :anchor => "new") %>
If you are working with a Request object and a restful route.
<%= link_to "Comment!", request_path(#request, :anchor => "new") %>
More details are available in the link_to helper documentation.