form-for

form_for with multiple controller actions for submit

我只是一个虾纸丫 提交于 2019-12-04 11:54:45
问题 How do I pass a url on the form_for submit? I'm trying to use one form with each buttons pointing to each controller actions, one is search and another one is create. Is it possible to have 2 submit buttons with different actions on the same form? <%= form_for @people do |f| %> <%= f.label :first_name %>: <%= f.text_field :first_name %><br /> <%= f.label :last_name %>: <%= f.text_field :last_name %><br /> <%= f.submit(:url => '/people/search') %> <%= f.submit(:url => '/people/create') %> <%

How to set up form for a hash in Rails?

百般思念 提交于 2019-12-04 11:31:40
问题 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

Rails 4: fields_for in fields_for

╄→гoц情女王★ 提交于 2019-12-04 08:07:43
问题 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

Rails not editable text field

荒凉一梦 提交于 2019-12-04 07:48:53
问题 I have a form_for written in the following manner: <div class="field"> <%= location.label :city %> <%= location.text_field :city, :disabled=>true%> </div> <div class="field"> <%= location.label :country %> <%= location.text_field :country, :disabled=>true%> </div> As you can see the 2 textfield are disabled because they are autofilled by a jquery function and I don't want let the user handle them. The problem is that in this way, the view doesen't pass that parameters to the controller

Ruby form using ajax with remote: true gives ActionController::InvalidAuthenticityToken error. Classic submission does not

帅比萌擦擦* 提交于 2019-12-04 06:15:38
I am writing a chat page for a RoR site. I have it all worked out in HTML and I am trying to implement it with ajax. There is a form for submission of messages. the form tag reads <%= form_for(@chat, remote: true, :authenticity_token => true) do |f| %> My entire view: <!DOCTYPE html> <html> <head> <title>Battleriskopoloy - Communications</title> </head> <body> <br> <div id="heading_1" align="center"> <br><span id="title"><strong>[ . . . Communications . . . ]</strong></span> </div><br> <div id="sidebar"><br> <% $chat_users.each do |user| %> <% user = User.find_by_username(user) %> <%= button

Radio button value in rails form_for not being part of params[:model]

末鹿安然 提交于 2019-12-04 05:52:57
I have an model (CustInfo), which contains 3 attributes: cust_name, age, gender. In my edit view, I use form_for for form submission: <%= form_for @cust[0] do |cust| %> <label for="cname">Customer name: </label> <%= cust.text_field :cust_name, :size => 20 %> <label for="age">Customer age: </label> <%= cust.text_field :age, :size => 5 %> <label for="gender">Gender </label> <%= radio_button_tag(:gender, "F", :checked => (:gender == 'female')? true:false) %><span style="float:left">F</span> <%= radio_button_tag(:gender, "M", :checked => (:gender == 'male')? true:false) %><span style="float:left"

form for nested resource

不问归期 提交于 2019-12-04 05:09:52
I have gone through tons of the form_for nested resource questions and can't get any of the solutions to work for me. I figured its time to ask a personalized question. I have two models, jobs and questions, jobs has_many questions and questions belong_to jobs. I used scaffolding to create the controllers and models then nested the resources in the routes.rb. root :to => "pages#home" resources :jobs do resources :questions end get "pages/home" get "pages/about" get "pages/contact" class Job < ActiveRecord::Base has_many :questions end class Question < ActiveRecord::Base belongs_to :job end

Creating a simple drop-down menu in Rails

一曲冷凌霜 提交于 2019-12-03 21:45:46
This really seems simple enough yet for some reason I'm missing something critical. I have a view: <% form_for :foo, @foo, :url => {:action => 'bar'} do |f|%> <%= f.collection_select :range, FooModel::MONTHS%> <%= submit_tag "Submit", :disable_with => "Submitting..." %> <% end %> I have a model: class FooModel < ActiveRecord::Base MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep'] end And I have a controller: def new @foo = FooModel.new end def index respond_to do |format| format.html # index.html.erb end end def bar if params[:foo] @foos = params[:foo].inspect end

nested routes and form_for and models using has_one and belongs_to

蹲街弑〆低调 提交于 2019-12-03 21:27:02
问题 How to map out has_one model with nested routes and how to add a form_for for /localhost:3000/users/1/profile/new,html.erb following RESTful database? User has one Profile. Models class Profile < ActiveRecord::Base attr_accessible :name, :surname belongs_to :user end class User < ActiveRecord::Base attr_accessible :email, :email_confirmation, :password, :password_confirmation has_secure_password has_one :profile, dependent: :destroy end resources :users do resources :profiles (note: has_one

Custom text for rails form_for label

余生颓废 提交于 2019-12-03 18:32:30
问题 I want to display a label in form_for : <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div> This generates the label "Name", but I want it to be "Your Name". How can I change it? 回答1: The second parameter to label helper will allow you to set custom text. <%= f.label :name, 'Your Name' %> Use Ruby on Rails Documentation to look up helper methods. 回答2: You can specify custom label text via i18n. In config/locales/en.yml , and assuming your user model is named user ,