ruby-on-rails-3

In Ruby on Rails, what's the difference between create and create! and API docs doesn't have it?

久未见 提交于 2020-01-09 10:24:53
问题 ActiveRecord has create and some people use create! ... Is it that create! can raise an exception while create doesn't? I can't find create! in the current Rails API docs... 回答1: Yes, create! will raise an exception on failure, create just returns false. Documentation here: http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-create-21 回答2: I have tested it in Rails 4.2.0 . In this version of Rails, it seems, #create! works as said in the other answer, but

In Ruby on Rails, what's the difference between create and create! and API docs doesn't have it?

感情迁移 提交于 2020-01-09 10:23:09
问题 ActiveRecord has create and some people use create! ... Is it that create! can raise an exception while create doesn't? I can't find create! in the current Rails API docs... 回答1: Yes, create! will raise an exception on failure, create just returns false. Documentation here: http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-create-21 回答2: I have tested it in Rails 4.2.0 . In this version of Rails, it seems, #create! works as said in the other answer, but

attachment_fu testing in rails 3

坚强是说给别人听的谎言 提交于 2020-01-09 09:50:35
问题 I am trying to write specs for a working file upload functionality using attachment_fu. However, the sample code given by the author for testing requires me to require action_controller/test_process so that I have access to ActionController::UploadedStringIO class. I have used this before in rails 2.x but for rails 3, it fails to locate the test_process file. How do I go ahead with testing the file upload functionality in rails 3? 回答1: I was able to use fixture_file_upload just fine with

Strip html from string Ruby on Rails

末鹿安然 提交于 2020-01-09 08:36:08
问题 I'm working with Ruby on Rails, Is there a way to strip html from a string using sanitize or equal method and keep only text inside value attribute on input tag? 回答1: There's a strip_tags method in ActionView::Helpers::SanitizeHelper : http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-strip_tags Edit: for getting the text inside the value attribute, you could use something like Nokogiri with an Xpath expression to get that out of the string. 回答2: If we want to

Iterate over a deeply nested level of hashes in Ruby

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-09 06:20:53
问题 So I have a hash, and for each level of the hash, I want to store its key and value. The problem is, a value can be another hash array. Furthermore, that hash can contain key value pairs where the value is again another hash array, etc, etc. Also, I won't know how deeply nested each hash will be. To give an example: { :key1 => 'value1', :key2 => 'value2', :key3 => { :key4 => 'value4', :key5 => 'value5' }, :key6 => { :key7 => 'value7', :key8 => { :key9 => 'value9' } } } ..And so on. What I

How to require a ruby file from another directory

假装没事ソ 提交于 2020-01-09 06:20:32
问题 I am trying to require a rake file that I have created inside another file I have. These two files are inside two different directories. I have require at the top of my first file with the name of the second file inside the quotes after the require. It is telling me that it can't load such file. Does that mean because its in different directory it can't find it? I tried sticking in the full path to the second file but it still can't load the file. Does anyone know how I can load the second

How to require a ruby file from another directory

跟風遠走 提交于 2020-01-09 06:20:07
问题 I am trying to require a rake file that I have created inside another file I have. These two files are inside two different directories. I have require at the top of my first file with the name of the second file inside the quotes after the require. It is telling me that it can't load such file. Does that mean because its in different directory it can't find it? I tried sticking in the full path to the second file but it still can't load the file. Does anyone know how I can load the second

using Liquid variables inside of a liquid tag call

给你一囗甜甜゛ 提交于 2020-01-09 05:22:28
问题 I made a custom link tag in Liquid and I am trying to be able to pass liquid variables into the call for that tag like so {{ assign id = 'something' }} // this value is actual dynamic while looping through data {% link_to article: id, text: 'Click Me!' %} // my custom tag However this results in the article parameter being passed in as 'id' instead of 'something' as per the assign statement above it. Does anyone know how to pass variables into tag calls? 回答1: I've recently solved this very

AJAX call not working in Ruby on rails

别等时光非礼了梦想. 提交于 2020-01-07 08:23:17
问题 I am learning ruby from the book "Agile web development with rails, 4th edtion" . I am using rails 3.2 and ruby 1.9.2 . I tried to make a call to the AJAX function of the card using the following code <%= button_to 'Add to Cart', line_items_path(product_id: product) , remote: true %> Then in the controller's "create" action method for line_item added respond_to do |format| if @line_item.save # format.html { redirect_to @line_item.cart,notice: 'Line item was successfully created.' } format

AJAX call not working in Ruby on rails

扶醉桌前 提交于 2020-01-07 08:23:02
问题 I am learning ruby from the book "Agile web development with rails, 4th edtion" . I am using rails 3.2 and ruby 1.9.2 . I tried to make a call to the AJAX function of the card using the following code <%= button_to 'Add to Cart', line_items_path(product_id: product) , remote: true %> Then in the controller's "create" action method for line_item added respond_to do |format| if @line_item.save # format.html { redirect_to @line_item.cart,notice: 'Line item was successfully created.' } format