form-helpers

Rails 4: Save Checkbox Results to Serialized Array

十年热恋 提交于 2021-01-26 09:20:50
问题 I have a Campaign model with a channel column. This channel will store a serialized array of chosen results via checkboxes. Here's the model.. app/models/campaign.rb class Campaign < ActiveRecord::Base serialize :channels, Array end app/controllers/compaigns_controller.rb class CampaignsController < ApplicationController def index @campaigns = Campaign.all.order("created_at DESC") end def new @campaign = Campaign.new end def create @campaign = Campaign.new(campaign_params) if @campaign.save

Rails 4: Save Checkbox Results to Serialized Array

坚强是说给别人听的谎言 提交于 2021-01-26 09:17:09
问题 I have a Campaign model with a channel column. This channel will store a serialized array of chosen results via checkboxes. Here's the model.. app/models/campaign.rb class Campaign < ActiveRecord::Base serialize :channels, Array end app/controllers/compaigns_controller.rb class CampaignsController < ApplicationController def index @campaigns = Campaign.all.order("created_at DESC") end def new @campaign = Campaign.new end def create @campaign = Campaign.new(campaign_params) if @campaign.save

Rails 3 - Make a “link_to” using selected value from collection select

只愿长相守 提交于 2020-01-05 08:13:23
问题 Hello everyone. I have a form in my rails 3 app, and one of the fields is a "collection select" like this <div class="field"> <%= f.label :provider_id, "Provider" %> <%= collection_select( :purchase_document, :provider_id, Provider.all, :id, :name) %> </div> The idea, is to be able to add a "link_to" using the selected value from the "collection select" i.e.: <div class="field"> <%= f.label :provider_id, "Provider" %> <%= collection_select( :purchase_document, :provider_id, Provider.all, :id,

How to make a check_box checked in rails?

好久不见. 提交于 2020-01-03 06:45:19
问题 I made checkboxes using the following rails form helper: <%= check_box("tag", tag.id) %> However, I need to make some of them checked by default. The rails documentation doesn't specify how to do this. Is there a way? How? 回答1: This has a very straightforward solution that is directly supported by check_box (at least as of rails 4, I didn't check older documentation) <%= check_box("tag", tag.id, {checked: true}) %> This will make the checkbox checked. Of course instead of true you will put in

CakePHP 2.0 Select form Multiple selected

房东的猫 提交于 2019-12-31 00:45:22
问题 I have have this dropdown menu where you can select multiple values. Now let's say I want to edit my info and make a dropdown menu with multiple selected values. Trying to figure out how it goes, but no results. Let's say I have: $selected = array(3, 4); $options = array(1,2,3,4); echo $this->Form->select('Attendees', $options,array('multiple' => true, 'selected' => $selected)); I've used this code, but nothing is selected. 回答1: Ok found a way, appearantly it needs to be like this: $selected

In rails, whether to use form helpers or not to?

女生的网名这么多〃 提交于 2019-12-30 06:39:14
问题 In rails, is it recommended to use form helpers? Internally, everything boils down to plain html then why not write the html directly? Performance will obviously be better in writing direct html than using helpers. Is using form helpers like a convention or something that rails developers must follow? 回答1: Define performance. Your performance or the applications? Say you have the same rhtml snippet spread out across your views. Say you have it in thousands of places. Maybe you even haven't

Why isn't Rails recognizing nil if blank in my form?

偶尔善良 提交于 2019-12-25 07:09:38
问题 I'm reworking a Rails 2 website. Right now, I'm getting an error, and I think it's because a value is being submitted as blank instead of .nil. However, my attempts to keep it nil don't seem to be working. I appreciate any help you have to offer. From Model, based on Make blank params[] nil NULL_ATTRS = %w( start_masterlocation_id ) before_save :nil_if_blank protected def nil_if_blank NULL_ATTRS.each { |attr| self[attr] = nil if self[attr].blank? } end View I've got jQuery that adds a value

How to create a delete form with RESTful routes in Rails.

不羁的心 提交于 2019-12-22 14:51:37
问题 I am trying to create a form that serves as confirmation for the destroy method on a controller. In my routes I have: resources :leagues do get 'delete', :on => :member end In my delete.html.erb, I have the following for: <% form_for current_league, :html => {:method => :delete} do |form| %> <%= form.submit 'Yes' %> <%= form.submit 'No', :name => 'cancel' %> <% end %> current_league is a helper function defined by: def current_league @current_league ||= League.find_by_id(params[:id]) end So

include bootstrap role attribute in rails form helper

大兔子大兔子 提交于 2019-12-21 01:14:06
问题 Twitter Bootstrap has this role attribute for forms: <form role="form"> How can I include the role attribute in Rails form helpers? I tried this, but it doesn't work: <%= form_for @user, class: "form-horizontal", role: "form" do |f| %> 回答1: You need to specify it as an html option: <%= form_for @user, html: {class: "form-horizontal", role: "form"} do |f| %> 回答2: form_for and form_tag have to be used differently: form_for: <%= form_for @user, html: { class: "form-horizontal", role: "form" } do

Add attribute to rails helper text field?

会有一股神秘感。 提交于 2019-12-19 05:35:08
问题 I have a rails helper text field from devise that uses html and atomatically validates the type="email" field. I want to add the attribute novalidate='novalidate' to it, but i do not know how.. Heres the code.. any suggestions? <%= form_for(resource, :as => resource_name, :url => session_path(resource_name) ) do |f| %> <p><%= f.label :login %><br /> <%= f.email_field :login %></p> 回答1: Just do: <%= f.email_field :login, :novalidate => 'novalidate' %> UPDATE -- If you want to add an attribute