ruby-on-rails-3.2

How to use read_attribute when manually defining getter/setter (attr_accessor or attr_writers)

只愿长相守 提交于 2019-12-08 13:34:45
问题 I set up a search model and want to require at least one field is filled out. I found a question that helps with the validation, Rails: how to require at least one field not to be blank. (I tried all answers, but Voyta's seems the best.) The validation is working, except when I want to redefine the getter/setter via attr_accessor or attr_writer . (I have virtual attributes on the form that need to be apart of the validation.) To figure out what the problem is, I tested with an attribute that

how to skip format validation in my case?

拜拜、爱过 提交于 2019-12-08 13:24:58
问题 I have a model User with an attribute "guest" (BOOLEAN) I want to skip format validation on email if user.guest == true or user.guest == nil, tried VALID_EMAIL_REGEX = /^(\S+)@([a-z0-9-]+)(\.)([a-z]{2,4})(\.?)([a-z]{0,4})+$/i validates :email, format: { :with => VALID_EMAIL_REGEX } unless :guest? but now even when user have guest == false or guest == nil validation is still skipping What could be wrong? another common validation works good: validates :email, presence: true, uniqueness: { case

How to add target to Rails form helper?

蹲街弑〆低调 提交于 2019-12-08 12:44:06
问题 I'm using the form_for helper and would like it to submit to an iframe. <%= form_for(section, :target => 'new_section_frame') do |f| %> ... <% end %> I should then be able to call $('form').submit() and have the form submit to the iframe. It would work except that the target isn't present in the output html. The Rails guide on form helpers doesn't have this particular example. 回答1: Options for the form_for helper that pertain to html have to be in the html key. Like so: <%= form_for(section,

Respond to all HTML requests with layout

雨燕双飞 提交于 2019-12-08 12:27:07
问题 I'm working on a Rails app that will be using a client-side framework with its own routing feature. I'd like to use pushState routing, so the Rails router will need to be configured to respond to such requests (easy enough). Is there an easy way to set all HTML requests with a valid route to be responded to with just a layout, without having to clutter up my views folder with a bunch of blank action.html.erb files? 回答1: Here's a way to intercept requests to valid routes and render a view for

Is there any method to convert boolean variable to integer in ruby

早过忘川 提交于 2019-12-08 11:48:17
问题 I have a ruby variable 'is_published'. I want to convert the value in 'is_published' to integer. Is there any method in ruby to do that? if is_published == true is_published = 1 else is_published = 0 end The above code works perfectly. Please help if there is any way to do this in a single line code. 回答1: class TrueClass; def to_i; 1; end; end class FalseClass; def to_i; 0; end; end 回答2: # using logical operators is_published = is_published && 1 || 0 # using ternary operator is_published = is

Testing http basic authentication with Cucumber and RSpec in Rails 3.2.3

家住魔仙堡 提交于 2019-12-08 11:36:58
问题 I want to test the build-in basic http authentication mechanism in Rails 3.2.3. I have tried to test the http authentication in both RSpec and Cucumber but with a failing step in both tools. In Cucumber I get the following message on running my Feature: When I perform HTTP authentication as "admin" with "test" # features/step_definitions/web_steps.rb:1 undefined method `env' for nil:NilClass (NoMethodError) ./features/step_definitions/web_steps.rb:3:in `/^I perform HTTP authentication as "([^

NoMethodError thrown when trying to use controller variables in js.erb file

时光总嘲笑我的痴心妄想 提交于 2019-12-08 11:03:00
问题 In my assets, I have a file called maps.js.erb , basically with only the following (debugging) content: alert("<%= @map.width %>"); This JS file is loaded through the show.html.erb view belonging to maps. <%= javascript_include_tag params[:controller] %> <h1><%= @map.title %></h1> … The HTML file itself shows the map's title, e.g. when browsing to /maps/1/ , since @map is defined in the controller. However, as soon as I include the JS file, I get the following error: NoMethodError in Maps

net-scp waiting until time-out in capistrano's git:wrapper task

独自空忆成欢 提交于 2019-12-08 10:45:12
问题 I'm using Rails 3.2 and Ruby 2.1 with capistrano 3.0.1 deployement, and I'm facing issue with the git:check task : douglas@bilbo:/var/www/odpf$ cap production git:check --trace ** Invoke production (first_time) ** Execute production ** Invoke load:defaults (first_time) ** Execute load:defaults [deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this

Rails 3.2.1 - cannot parse Cookie header: undefined method `size' for nil:NilClass

廉价感情. 提交于 2019-12-08 09:23:42
问题 I've updated the rails gem to version 3.2.1 and I created a dummy test application, but when execute I get this error: NoMethodError cannot parse Cookie header: undefined method `size' for nil:NilClass Anyone know help me? 回答1: This seems to be an issue with rack. I was able to fix it by pointing my Gemfile to master for the time being. gem 'rack', git: 'git://github.com/rack/rack.git' 回答2: Try clearing your cookies: http://www.aboutcookies.org/Default.aspx?page=2 回答3: For historical purposes

Rails Ajax Render Partial (voting system)

帅比萌擦擦* 提交于 2019-12-08 09:16:54
问题 I am having trouble re-rendering a partial through an Ajax request. I can currently pass other things through the js file such as alert() and even changing the html in the current div. But when I try and render the partial (rating, in this instance), nothing will refresh. The partials work fine in the sense that they render on pagination and on load, but not after I click an upvote or downvote. I'm using nested partials so that it renders _tracks first, then _rating. Here's what I have..what