ruby-on-rails-3

Rails - Where should this code go to stay true to MVC design?

こ雲淡風輕ζ 提交于 2019-12-25 16:42:21
问题 I'm new to Rails, and working my way through a couple of books, coding small test projects as I go. My latest dev app is a question and answer program. The idea is to have a database table full of questions. A user will bring up the web app and be presented with the first question. User enters answer into a form, and if it is correct, he gets the next question. Pretty simple. So, I've used generate scaffold to create a questions table and a users table. This works well - I can navigate to the

Trouble using 'helper_method' and rendering templates

柔情痞子 提交于 2019-12-25 15:13:11
问题 I am using Ruby on Rails 3 and I am trying to set an helper_method that should work just for a controller (example: AccountsController) and for all views related to that, also when its views are rendered in another views not related to that controller. I take inspiration from the Railcast "Restricting Access". In my accounts_controller.rb file I have # Just to know, I am using a User namespace... class Users::AccountsController < ApplicationController helper_method :show_authorization def

Uploading Multiple Images In Rails Using Loop

半世苍凉 提交于 2019-12-25 14:23:33
问题 Hello I have 2 models Products and ProductImage . Product has_many ProductImages . I am using paperclip gem. As you can see below in my binding.pry I have an array of the images locally. How do i upload these images and make a ProductImage. If i continue i get an error No handler found for "20090a.jpg" 69: def create_product_images => 70: binding.pry 71: params["product_images"].each do |image| 72: ProductImage.create(product_image: image, product_id: @form.product.id) 73: end 74: end [1] pry

Rails background worker always fails first time, works second

拈花ヽ惹草 提交于 2019-12-25 13:19:19
问题 I have a sidekiq worker that fires after an object is saved: class Post < ActiveRecord::Base attr_accessible :description, :name, :key after_save :process def process ProcessWorker.perform_async(id, key) if key.present? end def secure_url key.match(/(.*\/)+(.*$)/)[1] end def nonsecure_url key.gsub('https', 'http') end end The worker looks like the following (it's not complete yet... just testing): class ProcessWorker include Sidekiq::Worker def perform(id, key) post = Post.find(id) puts post

n:m self-join with ruby on rails active record

被刻印的时光 ゝ 提交于 2019-12-25 12:55:23
问题 I'm trying to accomplish the following with ruby on rails, and I have problems to get the model-configuration right: I would like to model possible connections between airports. I created the following models: > rails generate model airport city name > rails generate model connection airport_id destination_id But most of the n:m association examples deal with associations between two different models on the one hand, or self-referencing models on the other hand, but I want to model a n:m

Geocoder Gem: How to use between two different models?

风格不统一 提交于 2019-12-25 12:30:06
问题 I'm using the Geocoder and Will Paginate gems to sort my results based on the distance from a user's location and a kids' mom's address. My models are like this: class User has_many :kids geocoded_by :address end class Kid belongs_to :mom belongs_to :dad end class Mom has_many :kids has_many :dads, through: :kids geocoded_by :address end class Dad has_many :kids has_many :moms, through: :kids end Now, I'm trying to use the correct scope that I will use the Users.address and compare it to the

Remove hash from hash of hashes if value is duplicated

我的未来我决定 提交于 2019-12-25 12:13:47
问题 I have got such hash of hashes params = {collection_permissions_attributes: {'0' => {:collection_id => '1'}, '2' => {:collection_id => '1'}, '3' => {:collection_id => '4'}}} How can I remove completely from collection_permissions_attributes item which hash contains duplicated value? So from hash above I would like to have: params = { collection_permissions_attributes: {'0' => {:collection_id => '1'}, '3' => {:collection_id => '4'}}} Thanks in advance! 回答1: Here's another way, but it requires

Adding more data on saving 'has_many :through' associated records by keeping use of the “RoR magical\automatic way”

别说谁变了你拦得住时间么 提交于 2019-12-25 10:53:08
问题 I am using Ruby on Rails 3.0.7 and I followed this post about handling an " has_many :through => checkboxes " in which, in order to create user-group relationship records for membership purposes, is just passed a group_ids parameter (that is an array of id values) from check box input fields to the @user.save method. Using that code all works good in a "RoR magical\automatic way" (RoR set properly user_id values in the related memberships database table). However, on saving, I would like to

Template is missing in RoR App

…衆ロ難τιáo~ 提交于 2019-12-25 10:17:08
问题 I created an app "guestbook" in rails, I then created a controller using "rails g controller entries". After that I created index.html in app/views/entries/ and in it I wrote following :- <h1>Hello <%= @name %></h1> <%= form_tag :action => 'sign_in' do %> <p>Enter your name: <%= text_field_tag 'visitor_name', @name %></p> <%= submit_tag 'Sign in' %> <% end %> and in entries_controller.rb it is written :- class EntriesController < ApplicationController def sign_in @name = params[:visitor_name]

Adding more than one string to a controller find condition?

别来无恙 提交于 2019-12-25 09:01:14
问题 I have the following code in one of my Rails (3.1) controllers: @count = Call.where(:destination => current_user.destination_id).count @count_day = Call.where('created_at > ?', 1.days.ago).count @count_month = Call.where('created_at > ?', 30.days.ago).count They are all working as expected, but I am trying to somehow merge two of them together so it shows the count of calls created in the last 1 day, but only for calls with a destination that matches the current users destination_id value. I