ruby-on-rails-3

Why is Devise's “current_user” not available at the model layer?

混江龙づ霸主 提交于 2019-12-23 10:03:17
问题 There are many posts about Devise and the lack of availability of "current_user" for use in models. There are numerous work arounds posted here and elsewhere. However, i haven't found the answer to "why" in any of the posts. Is this a security issue? If not, why? 回答1: Because you don't have access to a session variable inside a model, it's not a Devise issue or something related to security. It's related to the MVC pattern used in Rails. 来源: https://stackoverflow.com/questions/5884132/why-is

Rails: Calling Devise authenticate_user! and handling invalid user/password exception

浪尽此生 提交于 2019-12-23 09:47:49
问题 I have a popup that will only allow to view/save some information if the user is authenticated. I am using devise. In the controller before_filter it checks if user is signed in and if not, show a sign in page. This sign in page is ripped down version of the site's sign in page, so that it fits nicely to the popup. On the authenticate action I call authenticate_user!. Everything works fine when the user enters valid credentials. But when the credential is invalid, devise automatically

How can i use accepts nested attributes with the HABTM?

試著忘記壹切 提交于 2019-12-23 09:47:47
问题 I have two models User and Category. class User < ActiveRecord::Base has_and_belongs_to_many :categories accepts_nested_attributes_for :categories end similarly class Category < ActiveRecord::Base has_and_belongs_to_many :users end i have a requirement where i have to add the categories to the categories table and add the reference so that i can get the categories related to user but if another user enters the same category then i have to make use of the id instead of creating new one. How

rails generate - “Could not find generator”

末鹿安然 提交于 2019-12-23 09:37:04
问题 I am following the Ruby on Rails tutorial by Michael Hartl. In section 6.3.1, I am trying to generate a migration file to add a Password field to my user model. Here is the code that I run: rails generate add_passsword_digest_to_users password_digest:string but this throws the error: Could not find generator add_passsword_digest_to_users. i have used the rails generate command before and it worked perfectly. I'm not sure why I am getting this problem now. version: Rails 3.2.8, ruby 1.9.3p194

rails3 - check_box_tag - how to make a Conditional Disabled

风格不统一 提交于 2019-12-23 09:36:56
问题 Given the following Rails 3 check_box_tag <%= check_box_tag 'XXXXXXX', 'true', true, (@setting.archived == true, :disabled => ? true : false ) %> How do I make the disabled setting conditional on @setting.archived ? Any ideas? Thanks 回答1: You've nearly got something that'll work. Try: <%= check_box_tag 'XXXXXXX', 'true', true, :disabled => (@setting.archived ? true : false ) %> remembering that (test ? a : b) is a single expression evaluating to a if test is true, and b if it's false. 回答2: To

rails 3.2 development mode is not displaying the full error page with backtrace etc

我与影子孤独终老i 提交于 2019-12-23 09:36:40
问题 I just upgraded to rails 3.2 Everything is working fine except error pages no longer show the normal development debug info. Instead it's showing the standard production error page (white background with red text in the middle: "We're sorry, but something went wrong. We've been notified about this issue and we'll take a look at it shortly." Is there a new setting or something I'm missing for rails 3.2? I've read over the upgrade instructions and do not see it mentioned anywhere. I tried

Rails 3 Factory Girl + Many to Many Relationships

不羁岁月 提交于 2019-12-23 09:34:06
问题 There aren't currently any up to date answers for this using Factory Girl 4.1 (that I could find) - how do you setup a many to many relationship inside of a factory? For instance I have Students and Classrooms which are in a many to many relationship using a join table, so far I had the following setup: factory :classroom do name "Foo Class" ... end factory :student do name "John Doe" ... end factory :student_with_classroom, :parent => :student do after(:build) {|student| student.classrooms <

Dynamic namespaced controllers w/ fallback in Rails

拈花ヽ惹草 提交于 2019-12-23 09:31:14
问题 I have a somewhat bizarre requirement for a new Rails application. I need to build an application in which all routes are defined in multiple namespaces (let me explain). I want to have an application in which school subjects (math, english, etc) are the namespaces: %w[math english].each do |subject| namespace subject.to_sym do resources :students end end This is great and it works but it requires me to create a namespaced StudentsController for each subject which means if I add a new subject

What does <%=h … %> means in Rails?

我只是一个虾纸丫 提交于 2019-12-23 09:30:14
问题 I found here the following syntax: <%=h @person.first_name %> What does the h means ? 回答1: It's for escaping the output of the tag to avoid cross-site-scripting. In rails 3, it's been changed to the default for a string (so rather than saying escape this string, you say, this is a safe string). http://api.rubyonrails.org/classes/ERB/Util.html#method-c-h 回答2: h is alias for html_escape method in Rails. If you do not escape the text using h , then someone can write javascript there and it will

Manually updating attributes mounted by Carrierwave Uploader

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 09:27:52
问题 I am unable to use model.update_attribute on an attribute that is mounted by a carrierwave uploader. The SQL statement wont accept the value and adds NULL to the placeholder. If I remove the mount_uploader statement from the model class it works as normal. I am troubleshooting things from the console and trying to add some attributes while seeding the DB and this is thwarting my efforts. Ideas? Thanks. Update: Relevant code: class Profile < ActiveRecord::Base belongs_to :user has_and_belongs