ruby-on-rails-3

Rspec Mocking: ActiveRecord::AssociationTypeMismatch

£可爱£侵袭症+ 提交于 2020-01-22 19:54:31
问题 I'm new to Rspec and trying to set up a test for a User Profile. Profile belongs_to User. Now, I have an API integration with a third party site that works through the User Model, but some of the information for that API link is contained in Profile, so I have an "after_update" filter on Profile that tells the parent user to save, which triggers an update of the API. I'm trying to write a test for this, and I'm getting an ActiveRecord::AssociationTypeMismatch. The reason is I'm using a mock

How to pass params to a block in Rails routes?

可紊 提交于 2020-01-22 16:38:30
问题 I am using Rails 3. And I'm wondering how to pass params to some blocks in routes.rb . What I'm trying to do is to make a catch all route, that check from slugs database the model name of it by the id. After getting the model name i pluralize it to get the controller name. match '/:id', :controller => proc { Slug.find_by_iid(params[:id]).model.pluralize }, :action => :show The table slugs model iid ----- ----- post 4d2c7de0c5abe7f8a9000007 item 4d2c7de0c5abe7f809000004 When I try to access

Rails mountable engine with isolate_namespace but without prefixed namespace on tables

你离开我真会死。 提交于 2020-01-22 13:53:48
问题 Is there a way to configure the isolate_namespace method to not use prefixed table names? class Engine < ::Rails::Engine isolate_namespace MyEngine end Additionally, an isolated engine will set its name according to namespace, so MyEngine::Engine.engine_name will be “my_engine”. It will also set MyEngine.table_name_prefix to “my_engine_”, changing the MyEngine::Article model to use the my_engine_articles table.Isolated Engine Docs When designing a prototype I ran into an issue where I need

Rails mountable engine with isolate_namespace but without prefixed namespace on tables

喜夏-厌秋 提交于 2020-01-22 13:53:44
问题 Is there a way to configure the isolate_namespace method to not use prefixed table names? class Engine < ::Rails::Engine isolate_namespace MyEngine end Additionally, an isolated engine will set its name according to namespace, so MyEngine::Engine.engine_name will be “my_engine”. It will also set MyEngine.table_name_prefix to “my_engine_”, changing the MyEngine::Article model to use the my_engine_articles table.Isolated Engine Docs When designing a prototype I ran into an issue where I need

Rails mountable engine with isolate_namespace but without prefixed namespace on tables

懵懂的女人 提交于 2020-01-22 13:52:33
问题 Is there a way to configure the isolate_namespace method to not use prefixed table names? class Engine < ::Rails::Engine isolate_namespace MyEngine end Additionally, an isolated engine will set its name according to namespace, so MyEngine::Engine.engine_name will be “my_engine”. It will also set MyEngine.table_name_prefix to “my_engine_”, changing the MyEngine::Article model to use the my_engine_articles table.Isolated Engine Docs When designing a prototype I ran into an issue where I need

Rails 3: How to get image path in Controller?

本秂侑毒 提交于 2020-01-22 10:28:07
问题 To get the image path in Controller I use the following method: class AssetsController < ApplicationController def download(image_file_name) path = Rails.root.join("public", "images", image_file_name).to_s send_file(path, ...) end end Is there a better method to find the path ? 回答1: ActionController::Base.helpers.asset_path('missing_file.jpg') Access Asset Path from Rails Controller 回答2: Not sure if this was added as early as Rails 3, but is definitely working in Rails 3.1. You can now access

Appending headers to Rspec controller tests

◇◆丶佛笑我妖孽 提交于 2020-01-22 09:25:40
问题 I'm trying to write out tests for a controller of mine that takes in requests from external services. So far this is my test: describe ApplyController do context 'when valid' do let(:parameters) do file = File.join File.dirname(__FILE__), '..', 'samples', 'Indeed.json' JSON.parse(File.read file) end let(:signature) { 'GC02UVj0d4bqa5peNFHdPQAZ2BI=' } subject(:response) { post :indeed, parameters, 'X-Indeed-Signature' => signature } it 'returns 200 ok if Request is valid' do expect(response

Sending HTTP request using ruby on rails

瘦欲@ 提交于 2020-01-22 08:12:09
问题 I'm new to ruby on rails and trying to test if I could do something like the below from my controller: curl -v -H "Content-Type: application/json" -X GET -d "{bbrequest:BBTest reqid:44 data:{name: "test"}}" http://localhost:8099 And what is the best practice to send JSON in your HTTP requests? 回答1: You could do something like this: def post_test require 'net/http' require 'json' @host = 'localhost' @port = '8099' @path = "/posts" @body ={ "bbrequest" => "BBTest", "reqid" => "44", "data" => {

Rails storing third party credentials.. Anyone know best practice?

陌路散爱 提交于 2020-01-21 21:53:25
问题 I've read a pile of other related questions... nothing really seems to answer the question I have. My application will integrate with several different third party sites. (ebay, paypal, google, amazon...) It is a product management system and it pushes products all over the place... Of course since it interacts with all these sites, it needs usernames, passwords, tokens.. ect.. Now I don't think it's really a good idea to store these things raw, but I still need to be able to get them raw, so

How to run ruby files?

随声附和 提交于 2020-01-21 20:22:25
问题 Example I have the file test.rb: puts "test test test" How do I run this file in the ruby console? 回答1: load 'test.rb' Do you mean Rails console? (Same thing, but the question is tagged rails.) 回答2: load("test.rb") should do the trick in irb . 回答3: On Mac you can run in three different ways from the terminal Method 1 On Terminal irb (Interactive Ruby Shell) for line by line execution then quit command to be out of irb. Method 2 As ruby is Interpreted language we can run by one command on