form-for

Difference between form_for and form_tag?

拈花ヽ惹草 提交于 2019-12-03 18:03:02
问题 I used this gem in my application, but I'm not sure the difference between the different implementation options for the gem: form_for form_tag with block form_tag without block Can anyone clarify? I understand that form_for is used when you wish to interact with a model, but what about the other two? 回答1: The differences are subtle, but important. form_for is a more advanced tool that yields an object you use to generate your form elements: <% form_for(@foo) do |form| %> <%= form.text_field(

Spree Dropdown boxes for variant option values

我怕爱的太早我们不能终老 提交于 2019-12-03 14:23:28
I'm learning Spree 3.0 and I have a setup a test shop that sells shorts. Shorts has multiple option types: Size, Color, Length I wanted to change the way it displays the variant options on the frontend from a radio checkbox to a drop down box. Currently, Spree displays the option types as radio buttons: I want to change this to use drop down menus for each option type, like this: I've tried the following: <%= select_tag "variant_id", options_for_select(@product.variants_and_option_values(current_currency).collect{ |v| ["#{variant_options(v)} #{variant_price(v)}", v.id] })%> But it simply

Rails Bootstrap how to format form_for (width grid collapses)

我的梦境 提交于 2019-12-03 10:16:37
问题 I'm trying to make a form with bootstrap-sass. I need to find the optimal configuration of bootstrap classes to make it look nice. It is not showing up the way I expect, specifically when the browser width is smaller than a certain size the spaces between labels and form fields collapse and it doesn't look good anymore. It would be ok if it only happens on small width, but that's not the case. I would really appreciate some help with this, really, what is the best way to format the form

Rails 3: using non-Active Record objects with form_for

本秂侑毒 提交于 2019-12-03 10:16:20
Is there any documentation, or advice? Sinetris You can use Active Model Take a look here . Look at the source code at Github . Using ActiveAttr is the more modern way of doing this. Gem: https://github.com/cgriego/active_attr Railscast: http://railscasts.com/episodes/326-activeattr 来源: https://stackoverflow.com/questions/4241779/rails-3-using-non-active-record-objects-with-form-for

Custom name for params hash from Rails form_for

喜欢而已 提交于 2019-12-03 09:27:50
Ordinarily, using form_for(@foo) means that on the back end of the form's action, you'll have the form data in params[:foo] , but in my case I'd like to have a custom namespace applied to these params, i.e. params[:bar] , not params[:foo] . I'm not talking about making the namespace longer by supplying the :namespace argument to the form_for method. To the contrary, my current name is overlong, and I want to shorten it. More importantly, I'm actually swapping a new model in place of an existing one, so the controller is filled with calls to params[:quoter] , whereas our new model supplies

How to upgrade the :update=>'div_id' option of remote_form_for from Rails 2 to Rails 3?

守給你的承諾、 提交于 2019-12-03 08:54:12
I can't figure out how to upgrade this code from Rails 2 to Rails 3: <% remote_form_for(item, :update => 'div_id') do |f| %> ... I tried this: <%= form_for :item, :remote => true, :url => { :controller => "items", :action => "create" }, :update => 'div_id' do |f| %> ... It creates the new item but it fails in updating the content within <div id="div_id"></div> tags. It seems Rails 3 no longer supports the " :update " option for a remote form_for . Any suggestion? You could use RJS, but that's being deprecated too (and for good reason). The simplified, best-practices way to handle this in Rails

How to set up form for a hash in Rails?

纵然是瞬间 提交于 2019-12-03 07:26:23
I have some data associated with a model that is in a hash. The hash is generated in the controller: @hash . What is the proper way to create a form for this data? I came up with the following code for the view: <% @hash.keys.each do |key| %> <div class="field"> <%= f.label key %><br /> <%= text_field_tag "hash_" + key, @hash[key] %> </div> <% end %> This generates the form, but it creates each hash item as a separate variable in the form. This doesn't seem to be the proper way to submit the data back. I would like to get the data back as a hash, and access it with params[:hash] . What is the

Change html form id generated by form_for rails 3.1

拜拜、爱过 提交于 2019-12-03 04:13:58
I have this form_for: <%= form_for [post, Comment.new,], :remote => true do |f| %> <%= f.text_area :content, :cols =>10, :rows => 1%> <% end %> <%= f.submit :class => "input_comment" %> That generate the next code html: <form method="post" id="new_comment" data-remote="true" class="new_comment" action="/post/4efcda9e1d41c82486000077/comments" accept-charset="UTF-8"><div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8"> <input type="hidden" value="ctVfDF/O4FIR91I7bC5MVezQmutOCkX3dcXe73uNPZY=" name="authenticity_token"> <textarea rows="1" name="comment[content

Ruby on Rails: Are “form_for(:product, …)” and “form_for(@product, …)” equivalent?

夙愿已清 提交于 2019-12-03 03:45:11
Is <%= form_for(:product, :url => {:action => 'update', :id => @product.id})) do |f| %> ... <% end %> and <%= form_for(@product, :url => {:action => 'update', :id => @product.id})) do |f| %> ... <% end %> exactly the same ? The @product in the form_for helper ships with more features. The :product only affects the input field's id and name. For example you have a text filed in the form: <%= form_for :product, :url => {...} do |f| %> <%= f.text_field :price %> <% end %> The generated html would look like: <input type="text" id="product_price" name="product[price]" /> The id and name value is

Rails 4: fields_for in fields_for

落花浮王杯 提交于 2019-12-02 21:59:09
I am learning RoR and i am trying to find how to set a fields_for in another one with has_one models like this: class Child < ActiveRecord::Base belongs_to :father accepts_nested_attributes_for :father end class Father < ActiveRecord::Base has_one :child belongs_to :grandfather accepts_nested_attributes_for :grandfather end class Grandfather < ActiveRecord::Base has_one :father end I used Nested Model Form Part 1 on Railscasts to get these: In children_controller.rb: def new @child = Child.new father=@child.build_father father.build_grandfather end def child_params params.require(:child)