ruby-on-rails-3.1

How to enter unique associations only?

坚强是说给别人听的谎言 提交于 2019-12-04 02:59:27
Running the following code to add an association enters multiple entries each time the code is ran: store.categories << category Is there a way to make it only enter unique associations between the two models in the db? Directly from the rails guides , hope it helps: class Person has_many :readings has_many :posts, :through => :readings, :uniq => true end Ignoring duplicates only seem to work with begin and rescue logic: begin stores.categories << category rescue puts "Duplicate entry ignored" end 来源: https://stackoverflow.com/questions/8032342/how-to-enter-unique-associations-only

Rails separate array items with comma inside partial

↘锁芯ラ 提交于 2019-12-04 02:09:29
问题 What is the most elegant way in Rails to create a comma-separated list inside a partial? I've just recently discovered that you can use partials to iterate through a collection sent from another view template. So in the view template I have: <% render @dvd.director %> Then in /view/directors/_director.html.erb: <%= director.director %> This actually does something like: @dvd.director.each { |d| puts d.director } Now, I know I could use a .join like so: <% @dvd.director.map { |t| t.director }

Trouble on using the i18n gem with partial template files

流过昼夜 提交于 2019-12-04 02:05:14
I am using Ruby on Rails 3.1 and I would like to know how to correctly handle internationalization related to partial template files. That is, ... ... in my app/views/users/flag.html.erb file I have: <%= t('.test_key1') %> <%= render :partial => "/users/flag_form" %> ... in my app/views/users/_flag_form.html.erb file I have: <%= t('.test_key2') %> If in my config/locales/views/users/en.yml file ( note : I am organizing files as stated in the official RoR guide ) I use en: users: flag: test_key1: Test 1 text test_key2: Test 2 text the Test 1 text is displayed in the "main" template ( app/views

Precompiling Assets with Rails 3.1

不想你离开。 提交于 2019-12-04 01:56:27
I'm pushing changes to Heroku and I get ... rake aborted! could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432? When I consult the Heroku documentation Here , it tells me I need to "configure a nonexistent database in your local config/database.yml" I'm not sure how to create a "nonexistent database"? I'm a beginner and would appreciate any help you can give me. Thanks. On Heroku, you must set this line in your config/application.rb: config.assets.initialize_on_precompile = false http://guides.rubyonrails.org

Trouble on eager loading “second degree” associated objects

无人久伴 提交于 2019-12-04 01:55:04
问题 I am running Ruby on Rails 3.1. I would like to eager loading "second degree" associated objects by applying some conditions, but I am in trouble. It seems that I already solved part of my issue by using: article_categories = article .categories .includes(:comments => [:category_relationships]) .where(:category_relationships => {:user_id => @current_user.id}) where involved classes are stated as the following: class Category < ActiveRecord::Base has_many :comment_relationships has_many

Ruby on Rails: How would i stay on the same page if the post is not saved?

不羁的心 提交于 2019-12-04 01:31:16
def create @addpost = Post.new params[:data] if @addpost.save flash[:notice] = "Post has been saved successfully." redirect_to posts_path else flash[:notice] = "Post can not be saved, please enter information." end end If the post is not saved then it redirects to http://0.0.0.0:3000/posts , but i need to stay on the page, with text input fields so that user can input data. post model class Post < ActiveRecord::Base has_many :comments validates :title, :presence => true validates :content, :presence => true validates :category_id, :presence => true validates :tags, :presence => true end new

javascript runtime in rails 3.1.0 and ruby 1.9.2. cant deal with with heroku. Did everything, but still does not working

给你一囗甜甜゛ 提交于 2019-12-04 01:30:47
问题 Please help, it's my studying project. It work locally but not online: Hello, it is said that i dont need special gems like "therubyracer" in rails 3.1.0 but it writes in "heroku logs": 2011-10-04T23:15:30+00:00 app[web.1]: ActionView::Template::Error (Could not fin d a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. 2011-10-04T23:15:30+00:00 app[web.1]: (in /app/app/assets/javascripts/rails.js )):.... i was trying to install "therubyracer" but

How do I redirect back to a page I'm currently on?

假装没事ソ 提交于 2019-12-04 01:23:35
In my users photo album page they see photos they have uploaded and each photo has a 'make default' link on it. When the user clicks make default , then the id of the photo is stored in the photo_id column of my profiles table. The issue is redirecting them back to: localhost:3000/settings/photo_gallery/:id Is there a way I can redirect back to the photo album using the id of the photo that has just been set as the default? Can Rails find what album I want to redirect to just by looking at the id of a photo, as a photo belongs to a photo album, and a photo album has many photos? I have the

Force SSL for specific routes in Rails 3.1

我的未来我决定 提交于 2019-12-04 01:14:34
I need to force SSL on all routes in my application except for landing#index . In config/application.rb , I have: config.force_ssl = true Then in landing_controller.rb , I have: force_ssl :except => :index However, all routes are still being redirected to https . Does anyone know how to conditionally force SSL in a Rails 3.1+ application? Solution: Add the following to your Gemfile : gem 'rack-ssl-enforcer' Add the following to your config/application.rb : config.middleware.use Rack::SslEnforcer, :except => [ /\/$/ ], :strict => true anshumans I asked a similar question on stackoverflow here

How to properly work with jQuery in Rails 3.1 asset pipeline?

不羁岁月 提交于 2019-12-03 23:07:51
I'm working on a hobby app and using some jQuery. The results are fine at the moment, but I'm a jQuery noob and I assume that there are some significant improvements that I can make to the code structure. Putting aside Coffescript for the moment, one thing that I've been wondering is how to properly use the model-specific .js files in the asset pipeline. For example, when working with my User model, I may have some code that I want to have run when the document is ready. Let's say I put that in $(document).ready(function() {...}); in the users.js file generated by Rails 3.1. The next day, I'm