Formtastic set class and id of form - ruby-on-rails

How can I set the class and id attribute for the form element through semantic_form_for?
The following code:
<% semantic_form_for (#meetingsearch), :class => "new_meeting_search", :id => "meeting_search" do |f| %>
gives me:
<form action="/meetingsearches" class="formtastic meetingsearch" id="new_meetingsearch" method="post">

This should do what you need (untested):
<% semantic_form_for #meetingsearch, :html => { :class => "new_meeting_search", :id => "meeting_search" } do |f| %>
For clarification, semantic_form_for wraps around Rails' built in form_for, so this is exactly how you do it in regular Rails forms also:
<% form_for #meetingsearch, :html => { class => "new_meeting_search", :id => "meeting_search" } do |f| %>

Related

SimpleForm: SyntaxError in Controller

Any idea on what could be wrong in this form?
Error:
vehicleTrack.html.erb:141: syntax error, unexpected keyword_do_block, expecting => ... params[:rangefrom_string]} do |f| #output_buffer.safe_appe... ... ^
This is my view:
<%= simple_form_for '', url: convertTrackToArea_path, :method => :post,
{ :controller => "vehicles",
:action => "convertTrackToArea",
:search => params[:search],
:rangefrom_string => params[:rangefrom_string]} do |f| %>
<%= f.input :areano, :label => 'Areano' %>
<%= f.button :submit, value: "Crear",:name => nil%>
<% end %>
I have not used simple_form so my guess is you are passing the last last argument incorrectly and it wont take a hash as argument and it detects the argument as key and searching for a value so throws error that => is missing, which is used to identify value in hash. So you can do something like this I suppose:
<%= simple_form_for '',
:url => url_for(:action => 'convertTrackToArea', :controller => 'vehicles',:search => params[:search],
:rangefrom_string => params[:rangefrom_string]),
:method => 'post' do |f| %>
since you are specifying controller and action you dont need to mention convertTrackToArea_path . If that route is already setup, you can just use that like in your posted question and remove controller and action name like:
<%= simple_form_for '', :url => convertTrackToArea_path(:search => params[:search],:rangefrom_string => params[:rangefrom_string]),:method => 'post' do |f| %>

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

Rails form_for with associations defined and multipart set to true not working

So I have one form working just fine:
<%= form_for([#document, #comment]) do |f| %>
And then I have another form where I need to include a Carrierwave upload that is like this:
<%= form_for([#document, #comment]), :html => { :multipart => true } do |f| %>
The first one works fine but the second one breaks by pointing to the form definition with the error:
undefined method `comments_path' for #<#<Class:0x0000010475dde8>:0x0000010475a440>
Any ideas? Running Rails 3.0.0 with Ruby 1.9.2p180
<%= form_for([#document, #comment], :html => { :multipart => true }) do |f| %>
<%= form_for (#comment, :url => [#document, #comment], :html => {:multipart => true}) do |f| %>
I believe that will fix it for you.

rails admin edit view path routes

I've got an admin section setup, but am having trouble getting the "update" route to work.
Getting error when hitting "update" via the edit view:
"No action responded to 2."
For some reason the route is responding to the :id as the :action.
Parameters:
Parameters: {"commit"=>"Update", "action"=>"2", "_method"=>"put", "admin"=>{"ended_at(1i)"=>"2010", "ended_at(2i)"=>"8", "ended_at(3i)"=>"22"}, "id"=>"edit", "controller"=>"admin/subscriptions"}
The edit view uri:
/admin/subscriptions/2/edit
Edit view:
<% form_for :admin, #subscription, :html => {:method => :put} do |f| %>
<p>
<%= f.label :ended_at %><br />
<%= f.date_select :ended_at %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
Route:
map.namespace :admin do |admin|
admin.resources :subscriptions
end
I assume I need to do something differently in the form_for method or maybe the routes, but everything I've tried isn't working.
Thanks for the help!!
It should be this:
<% form_for [:admin, #subscription] do |f| %>
By putting :admin and #subscription in square-brackets, this makes it into an array which is passed as the first argument to form_for. The benefit of this is if #subscription is a pre-existing record (as-in, one found by find, not created with new) then Rails will know to set the form method to PUT.
This works:
<% form_for :admin, #subscription, :html => {:method => :put}, :url => { :action => "update" } do |f| %>
Seems verbose though. Any better ideas?
Try
- form_for :subscription, #subscription do |f|
We're using formtastic here.

this.form is null in my Rails form

I have a form that will not submit:
<% form_for :venue, :html => { :id => "create_venue_form" } do |f| %>
<%= render :partial => 'venues/venue_form_fields', :locals => { :f => f } %>
<%= submit_to_remote 'add_venue_button',
'Save Venue',
{
:url => add_venue_path(#user.id),
:before => "alert(this.form);",
:html => {
:id => "add_venue_button"
},
:update => "venue_select"
}
%>
<% end %>
The problem is that this.form is null when prototype goes to serialize the form. I have put the alert statement in other forms and this.form popped up to be an HTML form element, so I know it should not be evaluating to null.
Does anyone know why this might be happening?
Thanks!
You can't create nested forms in HTML. You can put that nested form in a div and use serializeElements to serialize all inputs within that div.
If you are doing an AJAX update, which is how it appears, you should look at remote_form_for
Peer

Resources