Having trouble with rails current_page? - ruby-on-rails-3

Trying to use current_page? but keep gettting a no route error.
Here's the route I'm trying to match:
match 'firefly/signin', :to => 'Firefly::Sessions#create', :as => :firefly_signin
Here's what I'm using in the view:
<% unless current_page?(:controller => 'session') %>
But it doesn't like that. Adding :action => 'new' doesn't work either.

Try this:
<% unless current_page?(firefly_signin_url) %>

Related

Nested Routes Broken: Rails 3

I'm having a devil of a time getting this particular nested route to work. It's odd, because I've been migrating a number of routes to the new Rails 3 syntax and this one in particular just doesn't seem to work. Here goes.
I've got an object called "piece" which has a nested object called "piece_comment". Here's what the routes.rb looks like:
resources :piece do
resources :piece_rating, :as => :rating
resources :piece_comments, :as => :comments
end
And here is what piece/show.html.erb looks like, with a form to submit a piece comment:
<% #piece_comment = PieceComment.new(:piece_id => #piece.id, :user_id => current_user.id) %>
<%= form_for [#piece, #piece_comment] do |f| %>
<%= f.hidden_field 'piece_comment', 'user_id' %>
<%= f.hidden_field 'piece_comment', 'piece_id' %>
<%= f.text_area 'piece_comment', 'comment' %>
<%= f.submit_tag 'Post' %>
<% end %>
Now, what's weird is that I get the following error triggered by the "form_for" line:
undefined method `piece_piece_comments_path' for #<#<Class:0x007f80ec732a48>:0x007f80ec737ae8>
Shouldn't the :as in my routes file be sending it to piece_comments_path, and not piece_piece_comments_path? if I change it to :as => :foobar or something, I get the same error. So clearly the routes file would not seem to be working correctly. (Oddly, the behavior of the rating route seems fine.)
Any ideas for what might be wrong with the routing?
Altough I'm not sure it is the problem, resources should be plural in the routes.rb. Try with:
resources :pieces do
resources :piece_ratings, :as => :ratings
resources :piece_comments, :as => :comments
end
Use rake routes to see the name of the routes generated by the routes.rb.

Rails Routing issues

I have a website in people can browse artists and their albums. I have setup my routes like so:
match 'albums/[:id]/[:album_id]' => 'albums#show', :as => 'artist_album'
I tried setting up a nested route like:
resources :artists do
resources :albums
end
but I can't figure out how to achieve the routing like in the first example... but thats a different question... This is my code when trying to render artist_album_path
<%= link_to image_tag("#{album["Images"]["Album150x150"]}", width: "122", alt: "#{#term}", class: "float-left"), artist_album_path("/#{CGI::escape(album["Artist"]["Name"])}/#{CGI::escape(album["Title"])}") %>
I keep getting this error:
No route matches {:controller=>"albums", :action=>"show", :id=>"/Beastie+Boys/Licensed+To+Ill"}
Any idea on what I am doing wrong?
In routes.rb:
match 'albums/:id/:album_id' => 'albums#show', :as => 'artist_album'
In your view:
<%= link_to image_tag(...), artist_album_path(:id => ..., :album_id => ...) %>

no route matches even after adding route to routes.rb

This is the code in the view.
<% form_tag({:controller => 'users',
:action => 'test'}) do %>
<%= text_field_tag(:search_options, params[:search_options])%>
<%= submit_tag("Display text!")%>
<% end -%>
I have a file test.html.erb and have also added get "users/test" to routes.rb still i'm getting Error: No route matches "/users/test"
The form_tag method creates a form to be send using HTTP POST by default. You state that the route you define in your routes.rb is a GET. So you have two options to fix this problem:
Change your route to POST "users/test"
Change your form_tag call to: form_tag({:controller => 'users', :action => 'test'}, :method => :get)

Rendering the Devise edit Password Form

I'm trying to render the Devise edit password form within another view because I don't want to duplicate the edit pw logic.
I've tried the following (after generating the Devise views):
<%= render 'devise/passwords/edit' %>
<%= render 'devise/passwords/form' %>
And a number of other variations on render that all seem to give me the same error:
"ActionView::MissingTemplate in foo#foo
Missing partial devise/passwords/edit..."
This variation:
<%= render :file => 'devise/passwords/edit.html.erb' %>
Gave me some hope but the following error:
"undefined local variable or method `resource' for #<#:0x47ef0e0>"
around this line:
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
That makes me think I'm close (as that is code from the form that I want) but shouldn't that template be using the correct logic from the hidden Devise controller? Or do I need to do something in the routes file to get this to work?
Am I way off?
Try this:
<%= render :template => 'devise/passwords/edit',
:locals => {
:resource => my_user_model_variable,
:resource_name => my_user_model_name } %>
Where:
my_user_model_variable could be current_user
my_user_model_name could be "User"

How to change the controller path of a link in rails 3?

How do I get:
<%= link_to 'Back', originalcontrollers_path %>
to be:
<%= link_to 'Back', modifiedcontrollers_path %>
I already have my route updated with:
get "modifiedcontrollers", :to => "originalcontrollers#index"
So the "/modifiedcontroller" url works the same as "/originalcontroller". When creating links I need it to point to the new url. How would I do this?
I am not sure I understand you, but try this:
get "modifiedcontrollers", :to => "originalcontrollers#index", :as => :modifiedcontrollers
get "modifiedcontrollers/new", :to => "originalcontrollers#new", :as => :new_modifiedcontroller
...
so if you need all stack of routes:
resources :modifiedcontrollers, :controller => :originalcontrollers
so now this will work as expected
<%= link_to 'Back', modifiedcontrollers_path %>