Ruby on Rails - How to pass hash parameter to a partial view? - ruby-on-rails

'shared/subscription' %>
To call this partial view:
<% form_for(:subscription, :url => city_subscriptions_path(#city)) do |form| %>
<%= form.hidden_field :city_id, :value => #city.id %>
<%= form.text_field :email, :size => 30 %>
<%= form.submit "Email Me" %>
<% end %>
Since I am using this partial view on different places, how do I alter the caller so it will pass a hash for the form_for helper? So it would be like this when the helper is called:
<% form_for(:subscription, :url => city_subscriptions_path(#city), :html => {:id => 'main_input' }) do |form| %>
<%= form.hidden_field :city_id, :value => #city.id %>
<%= form.text_field :email, :size => 30 %>
<%= form.submit "Email Me" %>
<% end %>

<%= render :partial => "shared/subscription", :locals => {:foo => "bar", :foofoo => ["bar", "bar"]}
In your partial view, use them:
<%= foo #this outputs "bar" %>
<%= foofoo.to_s %>

Related

Rails remote form submitting duplicate form data

When I submit a remote form in rails, the form POST duplicate data, even though if I manually serialize the form using jQuery shows that there are no duplicates.
What could be causing this?
*Normally this wouldn't be a problem, but one of the form data is an array, as a result there are duplicate value in the submitted array.
Result from chrome's network inspector
Result from jQuery
UPDATE
Here is the gist of my form (removing the html markups):
<% #order.available_payment_methods.each do |method| %>
<%= radio_button_tag "order[payments_attributes][][payment_method_id]", method.id, method == #order.available_payment_methods.first %>
<%= Spree.t(method.name, :scope => :payment_methods, :default => method.name) %>
<%= render :partial => "spree/checkout/payment/#{method.method_type}", :locals => { :payment_method => method } %>
<% end %>
# partial
<% param_prefix = "payment_source[#{payment_method.id}]" %>
<%= label_tag "name_on_card_#{payment_method.id}", Spree.t(:name_on_card) %><span class="required">
<%= text_field_tag "#{param_prefix}[name]", "#{#order.billing_firstname} #{#order.billing_lastname}", { id: "name_on_card_#{payment_method.id}", class:'form-control'} %>
<% options_hash = Rails.env.production? ? {:autocomplete => 'off'} : {} %>
<%= text_field_tag "#{param_prefix}[number]", '', options_hash.merge(:id => 'card_number', :class => 'required cardNumber form-control', :size => 19, :maxlength => 19, :autocomplete => "off") %>
<%= text_field_tag "#{param_prefix}[expiry]", '', :id => 'card_expiry', :class => "required cardExpiry form-control", :placeholder => "MM / YY" %>
<%= text_field_tag "#{param_prefix}[verification_value]", '', options_hash.merge(:id => 'card_code', :class => 'required cardCode form-control', :size => 5) %>
<% if #order.bill_address %>
<%= fields_for "#{param_prefix}[address_attributes]", #order.bill_address do |f| %>
<%= render :partial => 'spree/address/form_hidden', :locals => { :form => f } %>
<% end %>
<% end %>
<%= hidden_field_tag "#{param_prefix}[cc_type]", '', :id => "cc_type", :class => 'ccType' %>
UPDATE Submitting the form without remote: true fixes the problem (BUT I NEED IT!)
Add onsubmit="return false();" on your form it will make sure that form is not submited through default form submission. It will be submitted through ajax as you are adding remote: true

Field named value in Rails

I'm using 'rails-settings-cached' gem and I want to add ability of creating/updating settings in backend. Model has fields: 'var', 'value', etc... As form builder I'm using Formtastic-bootstrap.
When I doing f.input :value I get no implicit conversion of nil into String. I think that the reason is that 'value' is a keyword.
How can I solve my problem?
Thank you!
UPD:
<%= semantic_form_for [:admin, #setting], :html => {:class => "form-horizontal"} do |f| %>
<%= f.semantic_errors :var, :value%>
<%= f.inputs do %>
<%= f.input :var %>
<%= f.input :value, :as => :text %>
<% end %>
<%= f.actions :class => 'form-group' do %>
<div class="col-lg-offset-2 col-lg-8">
<%= f.action :submit %>
<%= f.action :cancel %>
</div>
<% end %>

hidden_field syntax problems

I have been trying to pass what round the users intends to go to by which button they click through a hidden field that passes in the round number.
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
With this code I constantly get 2 passed through as the round in my pick_page. It is obviously skipping over the first hidden field. How can I get it so 'f.submit' submits the round number that is associated with its class.
We just had the same problem! We fixed it creating 2 diferent forms right next to each other.
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
Change it to
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<% end %>
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
You can just remove the hidden fields and add a name and value attributes on your submit button.

how to write seperate controllers for rails single page app?

I am creating list of forms say
Questions form
Answers form
Hints form
All these have different controller and view, question_controller , answers_controller, hints_controller.
Now I need to fetch all these views in tabbed UI in home page (say home_controller , home#index)
I tried render : partial ,render :template also with locals , I can't achieve.
It can be easily done by moving all the object to same controller ( home_controller , but i am not sure about this approach , since it will make home controller too complicated to manage ) , but I need to keep this in separate controllers (question_controller , answers_controller, hints_controller) and render it to same page. I am using client side validation, simple form gems.
Below is my question controller
class QuestionsController < ApplicationController
def index
#question = Question.new
#question_status = []
#question_mode = []
#question_type = []
#question_lookups = Lookup.where({:lookup_for => "question"})
#question_lookups.each do |lk|
case lk.lookup_type
when 'mode'
#question_mode << lk
when 'status'
#question_status << lk
else
#question_type << lk
end
end
#caa = Questioncaa.new
end
end
Question View ( with Simple form )
<%= simple_form_for #question, :validate => true do |q| %>
<%= q.input :question_info, :as => :ckeditor, :input_html => { :toolbar => 'Easy', :width => 750 } %>
<%= q.input :question_source %>
<%= q.input :is_mobile %>
<%= q.input :is_similar_question %>
<%= q.input :is_boss_question %>
<%= q.input :is_racing_question %>
<%= q.input :is_speed_question %>
<%= q.input :difficulty_level %>
<%= q.input :ideal_time %>
<%= q.input :lookups, :collection => #question_mode, :value_method => :id, :label_method => :lookup_value,:prompt => "Choose Mode", :label => :QuestionMode %>
<%= q.input :lookups, :collection => #question_status, :value_method => :id, :label_method => :lookup_value,:prompt => "Choose Status", :label => :QuestionStatus %>
<%= q.input :lookups, :collection => #question_type, :value_method => :id, :label_method => :lookup_value,:prompt => "Choose Type", :label => :QuestionType %>
<%= simple_fields_for #caa do |c| %>
<%= c.input :needs_hints %>
<%= c.input :needs_video_solution %>
<%= c.input :needs_tips_tricks %>
<%= c.input :needs_formulae %>
<%= c.input :needs_key_concepts %>
<% end %>
<%= q.button :submit %>
<% end %>
Home View
<div class="tab-content">
<div class="tab-pane active" id="learning_map">
<!-- I need to acheive this -->
<%= render :template => "learning_map/index" %>
</div>
<div class="tab-pane" id="questions">
<!-- I need to acheive this -->
<%= render :template => "questions/index", :collection => #question_mode %>
</div>
<div class="tab-pane" id="answers">.
<!-- I need to acheive this -->
<%= render :templates => "answers/index" %>
</div>
</div>
Pls advice me , it will be very helpful. Thanks for reading this.
You should change logic of your templates a little (here is example for 'question' view):
1) split your question template into 2 files:
- header with form declaration of simple_form_for
- _form.html.erb file with <%= fields_for #question do |q| %> and list of your questions fields like <%= q.input %>
2) add <%= render :partial => 'form' %> in your header file
3) use <%= render :partial => 'question\form' %> in your home view template
4) dont forget to initialize #question variable in home_controller.

Ruby on rails form_tag - sending model parameters to controller

Controller: #micropost = Micropost.new(params[:micropost])
But this form_tag is sending me params[:content] instead of params[:micropost][:content]
<%= form_tag( {:controller => :microposts, :action => :create}, :remote => true) do %>
<%= text_area_tag :content, "", :size=> "20x2" %>
...
...
...
<%= submit_tag "submit" %>
<% end %>
server:
Processing by MicropostsController#create as JS
Parameters: {"utf8"=>"✓", "content"=>"sdfsdf", "commit"=>"submit"}
You have to do either of the following
<%= text_area_tag "micropost[content]", "", :size=> "20x2" %>
OR
<%= form_for :micropost, :url=>{ :controller => :microposts, :action => :create}, :remote => true do |f| %>
<%= f.text_area :content, "", :size=> "20x2" %>
<% end %>
You have to avoid mixing form_for and input_tag.
When you declare a form_for #an_object do |form|, the best practice is to use form.text_area :content when :content is an attribute of your #an_object.
In this case, you can also write: text_area_tag "an_object[content]", but it's a little more dirty.

Resources