I'm writing a view spec, and it renders a view that contains the line (in haml):
=link_to new_post_path
but the spec fails with:
ActionController::RoutingError: No route matches {:action=>"new", :controller=>"post"}
I'm trying to stub the new_post_path method, as it's not actually important to the view spec, but haven't had any luck.
I've tried, within my spec, the following two variations without any luck:
stub!(:new_post_path).and_return("this path isn't important")
controller.stub!(:new_post_path).and_return("this path isn't important")
If you're not in a view spec, but find yourself needing to stub a path helpers, the following syntax works as of Rails 4.2.5; you'll need to receive_message_chain instead as described here:
https://github.com/rspec/rspec-mocks/issues/1038
To wit:
allow(Rails.application).to receive_message_chain(:routes, :url_helpers, :new_post_path).and_return("this path isn't important")
The method new_post_path comes from the Rails.application.routes.url_helpers module. You need to stub the method on that module
Rails.application.routes.url_helpers.stub(:new_post_path).and_return("this path isn't important")
allow(view).to receive(:new_post_path).and_return("this path isn't important")
that is rspec 3.2 syntax
I guess the old syntax would be
view.stub(:new_post_path).and_return("this path isn't important")
Related
here it's routes.db
resources :licenses do
resources :sublicenses do
resources :time_licenses
end
end
Then there is a client application that calls time_licenses_controller for creating new time_licenses, the controller responds with a json file, but i don't need to show any view.
Somewhere else instead i need to show to the client an index file including all time_licenses for every sublicense.
That's the path
license/:id/sublilicense/:id/time_lincenses
Now i have a problem with the routes. When i call the create on time_licenses_controller i get this error:
No route matches "/time_licenses.js"
that i can solve changing the routes.db file like this
resources :time_licenses
resources :licenses do
resources :sublicenses
end
but in that case i get the same error linking the index view.
What do you suggest me? Do i have to create two different controllers?
Since you are using nested resources, you will always need to specify license and sublicense while specifying the path to timelicense.
Your path helpers will be:
license_sublicense_timelicense_path(#license, #sublicense, #timelicense) and so on
You can get the path names for the timelicense by
rake routes
Refer rails guides - nested resources for any doubts.
I'm using the Linkedin gem to pull profile information for RoR 3.
Gem: https://github.com/pengwynn/linkedin
API Doc: https://developer.linkedin.com/documents/profile-fields#positions
Everything works except when I get to a property with a dash in the name.
<%=position.title %> displays correctly but<%= position.start-date %> return a NoMethodError in Users#show - undefined method start.
I've tried different operations like "startDate", "start_date", quotes around "start-date" but none have worked.
Is there a proper way to escape the dash/hyphen in the property name?
The expression in your ERB will be parsed as subtracting the value of the date variable from the result of a call to the start() method of the position object. Hyphens aren't valid in identifiers within Ruby.
I'm not familiar enough with the LinkedIn gem to suggest a solution, except to say that since it's based on an XML API, you should look for a way to manually pull data out of a tag pair. Most similar gems offer such a method. Also, this is a great case for using IRB as an exploratory tool: fire up an IRB session and see what happens when you call position.methods, after properly creating the position variable of course. My guess would be that you'll see something in that list which suggests an answer.
Looks like it returns a Hashie::Mash which converts keys, with a few extra rules:
https://github.com/pengwynn/linkedin/blob/master/lib/linked_in/mash.rb
You said you'd already tried position.start_date right? That should work. But if not, have you tried position['start-date'] or position['start_date'] one of those two should also work, since it's a Mash.
Not sure what the issue is here, but I have a basic line in my routes.rb:
resource :videos
But I don't see all the paths. Namely:
GET /videos/:id
I only see the following when running "rake routes":
videos POST /videos(.:format) videos#create
new_videos GET /videos/new(.:format) videos#new
edit_videos GET /videos/edit(.:format) videos#edit
GET /videos(.:format) videos#show
PUT /videos(.:format) videos#update
DELETE /videos(.:format) videos#destroy
What am I missing? Thanks!
You make videos a singular resource, but videos is a collection so you have to do :
resources :videos
http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
Change your line to resources :videos, and the missing route will magically appear
I have a model higher_ed.rb that is a subclass of group.rb. I'd like to back up higher_ed.rb with a Controller. The fact that there are specifics as to plural in the controller and singular in the model confuses a beginner like me enough. Throwing the underscore adds to that, so any help would be appreciated.
I did take a crack at it but got an error of uninitialized constant HigheredsController. That makes sense because my controller is camel cased as HigherEdsController. What am I doing wrong?
Inside of my file is the following:
class HigherEd < Group
end
I tried to run the following to create the appropriate controller:
rails g controller higher_eds
I then put the following in my routes.rb:
resources :highereds
Your routes file should be:
resources :higher_eds
From a single view file containing e.g. LaTeX code with ERB inserts, I would like to be able to:
render to a LaTeX source file, by evaluating the ERB
render to PDF, by compiling the previous result using a custom latex_to_pdf() function
The first case can be achieved by registering a template handler:
ActionView::Template.register_template_handler :latex, LatexSource
where LatexSource is a subclass of ActionView::Template::Handler implementing call(template) or compile(template).
This allows a view file "action.tex.latex" to be accessed and processed correctly as "controller/action.tex".
The second case seems much harder though:
how can the request "controller/action.pdf" be sent to the template handler as if it was "controller/action.tex", and pass the result through latex_to_pdf() before sending the response to the user?
Many thanks
Couldn't you just register another template handler :pdf whose compile method looks similar to this?:
def compile
latex_to_pdf LatexSource.compile(template)
end
Update:
Ok, right, this results in the need of having the view duplicated (action.tex.latex, action.tex.pdf).
Next idea:
respond_to do |format|
format.latex
format.pdf { render :file => latex_to_pdf(render) }
end
As far as I can remember, render returned the rendered template as String in Rails 2.3.
I don't know how it behaves in Rails 3.
You could experiment with render or _render_template. If this works, we could think about how to make this more dry and comfortable for multiple actions.
I didn’t use it myself (yet), but it looks as if https://github.com/jacott/rails-latex could do the job for you.