ruby-on-rails-3

Rails 3 translations within models in production

末鹿安然 提交于 2019-12-31 05:05:12
问题 I'm trying to translate an app into Japanese and everything was going smoothly until I put it into production. As cache_classes is now true any translation within a model reverts to the default locale. I know I'm probably supposed to target the translations directly in the yml file but I'm not sure how I would do that for the following simplified code: class TimeseriesForecast < ActiveRecord::Base @@field_names = { :location_name => I18n.t('forecast_timeseries.location_name'), :local_date

ActiveRecord::RecordNotFound - Couldn't find User without an ID

送分小仙女□ 提交于 2019-12-31 05:03:06
问题 Following Hartl's RailTutorial for Rails(3.2). I keep getting this error message or oddly a "The connection was reset" message: ActiveRecord::RecordNotFound in UsersController#show Couldn't find User without an ID I'm trying to log in and after successfully logging in, I want the application to redirect the user to user/show.html.erb. I believe that I'm passing the :id parameter in the show method in the users controller. Any tip/help would be appreciated! I've pasted some relevant files

What's the rails way to load other models collections for new, edit update and create actions?

前提是你 提交于 2019-12-31 05:02:07
问题 How is the best way to load Category model, for ProductController in new, edit update and create actions Product has categories collection class Product < ActiveRecord::Base has_many :categories end Always for new, edit, create and update actions, I need to load the categories collection to populate a check_box_tag list The "baby steps" for this situation is: class Admin::ProductsController < Admin::AdminApplicationController def new @product = Product.new @categories = Category.all end def

how to use link_to with ajax in rails 3.2.1

喜夏-厌秋 提交于 2019-12-31 04:44:06
问题 I m using Rails 3.2.1 . how to use link_to with remote=>true My Method in Controller def clickme @clk = "you click me" respond_to do |format| format.js { render :layout=>false } end end My View In my new.html.erb file <%= link_to "click here", {:action=>"clickme"}, {:remote => true, :id=>"clk"} %> <div id="allclick"> <%= render :partial => 'goclick' %> </div> _goclick.html.erb <%= @clk %> clickme.js.erb $("allclick").update("<%= escape_javascript(render(:partial => "goclick")) %>"); On my web

saving file after passing parameter

邮差的信 提交于 2019-12-31 04:02:09
问题 Here is the parent question: save string to file I want to pass the parameter which will be saved in file(.csv) after clicking button. @bigtable is a table with strings in each row. Here is the code in my show.html.erb: ...some code here... <%= form_tag do %> <% text_field_tag, id = "bigtable", value = @bigtable.to_s %> <%= submit_tag 'Zapisz' %> <% end %> and my controller method: def savefile @bigtable = param[:bigtable] @bigtable.join("\n") File.open("path/to/file", "w") { |file| file

How to find distinct years from a table with multiple years in Rails

旧时模样 提交于 2019-12-31 01:58:07
问题 I have a table with several hundred records, each one containing a datetime column. What is the most efficient way to find the distinct years from that datetime field? 回答1: Using Ruby (this would require loading all the records, though): Model.select("my_datetime").map{ |item| item.my_datetime.year }.uniq Using SQL (this example is for PostgreSQL, I can update it with your specific database): # PostgreSQL Model.select("distinct(extract(year from my_datetime))") # MySQL Model.select("distinct

Rails : Adding an admin role using devise who can see all the users

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-31 01:50:16
问题 I need to create an admin role using devise for my app. I've created basic authentication using devise . I have a devise user model in my app but now i need an admin who can show edit and destroy all the users. I tried following the tutorials but none of them helped. I am using rails 3.0.10 and ruby 1.9.2-p290. 回答1: You just define role.rb first by creating migratioin rails g model role name:string then in role.rb class Role has_one:user end And in user model class user belongs_to :role end

Redirect to canonical route in without trailing slash in Rails 3

做~自己de王妃 提交于 2019-12-31 00:50:27
问题 On Rails 3, I'm trying to redirect from a URL without a trailing slash to the canonical URL that has a slash. match "/test", :to => redirect("/test/") However, the route above matches both /test and /test/ causing a redirect loop. How do I make it match only the version without the slash? 回答1: You can force the redirect at the controller level. # File: app/controllers/application_controller.rb class ApplicationController < ActionController::Base protected def force_trailing_slash redirect_to

How can i join with a derived table?

一曲冷凌霜 提交于 2019-12-31 00:49:09
问题 The sql statement is like this: select posts.id, posts.title from posts inner join (select distinct post_id,created_at from comments order by created_at DESC limit 5 ) as foo on posts.id=foo.post_id order by foo.created_at DESC; I want to get a rails 3 sql statement equivalent to the above one. What i tried is given below: The following sql query gives similar result. select distinct post_id, created_at from comments order by created_at DESC limit 5 @temp = Comment.select('distinct(comments

Call Module function from Controller (NoMethodError)

我的梦境 提交于 2019-12-30 22:29:12
问题 So I have a module "MiddleMan" I am able to call it just fine in the rails console but in the controller I am getting a NoMethodError In the controller I have: class SignUpController < ApplicationController include MiddleMan def page_one @package = MiddleMan::read_catalog("a", "b", "c") end end And in the middleman.rb module I have: module MiddleMan def read_catalog(package, payment, coupon) Package.new(:price => "4.99") end end Any thoughts? 回答1: Since you included the module the instance