ruby-on-rails-3

Rails: Load just one attribute not a whole model

☆樱花仙子☆ 提交于 2020-01-03 07:29:32
问题 Lets say I have a model, Foo , which is big and has lots of components. For a given Ajax query I'm only interested in one particular attribute, bar , which is a column in the foos table. Is there a simple way I could load just that attribute, and not bother with retrieving the rest of the record? For instance if all I want to know is the bar for Foo with id# _ _, how could I retrieve that? 回答1: You can return only specific columns by calling the select method with a string containing the

Pass Parameters in render - Rails 3

我与影子孤独终老i 提交于 2020-01-03 07:27:28
问题 I've seen a couple questions on this but haven't been able to solve it... I'm trying to pass a parameter while rendering a partial (similar to domainname.com/memory_books/new?fbookupload=yes) Right now, I use this line: <%= render :partial => '/memory_books/new', :fbookupload => "yes" %> and in the partial, I have tried to get the content of fbookupload by using: <%= fbookupload %> which gives an "undefined local variable" error and <%= params.inspect %> which does not show fbookupload as a

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

Ruby on Rails receiving Internal Server Error: expected Array (got String) for param

拜拜、爱过 提交于 2020-01-03 06:17:19
问题 I have a loop in a form and I'm trying to create multiple data with same name but I'm getting an error with my syntax. I had another question to the same application HERE where I had a mass-assignment error because of the numbers in the array <%= form_for [@hourable, @hour] do |f| %> <% (1..7).each do |i| %> <%= select_tag "hour[#{i}][day]", options_for_select(days_hours) %> <%= select_tag "hour[#{i}][open_time]", options_for_select([ ... %> <%= select_tag "hour[#{i}][open_time]", options_for

Does rti/FastXml, or anything like it, work with rails 3 to speed-up xml rendering?

守給你的承諾、 提交于 2020-01-03 05:58:18
问题 We have a REST api implemented in rails 3 , which is using the yajl back-end for json . It is currently generating json responses significantly faster than xml when serializing more than about 20 rows. My next thought was that there must be a similar C-library drop-in for rail 3, which would use libxml or similar for to_xml , just as we're already using libyajl for to_json . To date, I've found only the rti/FastXml project: https://github.com/rti/FastXml It claims to be what I want, but hasn

After installing ruby 1.9.3 rails 3.2.1, getting OpenSSL::SSL::SSLError: on simple requests

情到浓时终转凉″ 提交于 2020-01-03 05:46:33
问题 Got ruby 1.9.3/rails 2.3.1 installed via rvm (finally worked after installing openssl via rvm pkg.) but now getting OpenSSL::SSL:SSLError trying to connect to github (and probably anything else): -->irb 1.9.3-p0 :001 > require 'open-uri' 1.9.3-p0 :002 > open('https://github.com/') OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed -- Tried the solution in "Certificate verify failed" OpenSSL error when using Ruby 1.9.3 but

How can I generate this schema with Rails?

我是研究僧i 提交于 2020-01-03 05:43:06
问题 I have a schema for a relational database, which I'd like to generate scaffolding for in my Ruby on Rails 3.2.8 project. I've found the documentation pretty confusing though, and my efforts so far have failed, so my question is exactly how I'd go about generating the needed scaffolding/models for the following schema: USER name:string email:string ----- has_many: posts TAG name:string ----- belongs_to_and_has_many: series belongs_to_and_has_many: posts POST title:string body:text -----

Collecting first segments of all routes in rails 3

那年仲夏 提交于 2020-01-03 05:26:17
问题 I'm trying to implement routes where the first segment is the profile's alias or id: resources :profiles, :path => '' do ... end And I need to validate that alias is not already taken by first segments of other(higher) routes. What I have now is: validates :alias, :exclusion => {:in => Rails.application.routes.routes.map{|r| r.path.spec.to_s.split('(').first.split('/').second}.compact.uniq }, .... In development everything is ok. In production Rails.application.routes.routes.map... returns

Map URL “/users/id” to “/dashboard” in Rails 3?

﹥>﹥吖頭↗ 提交于 2020-01-03 05:14:14
问题 How can I map the url /users/:id to /dashboard? Thanks 回答1: Where is the id parameter with URL /dashboard ? Or is this about the "current" user? If these pages are about different users, one from parameter, the other from session. Then you actually need different controllers for these 2 URLs. Here is how I would do it: routes.rb match "dashboard" => 'users#show', :defaults => { :id => -1 } UsersController.rb def show @user = User.find(params[:id] == -1 ? current_user_id : params[:id]) ... I

How to split routes.rb into multiple files [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-03 05:10:12
问题 This question already has answers here : Splitting Routes File Into Multiple Files (4 answers) Closed 6 years ago . I have huge routes.rb file and I want to split into multiple manageable files. As suggested in following article, I have created separated folder for routes and I have created multiple routes files in this folder Link: http://rails-bestpractices.com/posts/73-split-route-namespaces-into-different-files routes.rb routes/user.rb routes/manager.rb routes/admin.rb routes/anonymous.rb