Dynamic Find By In Rails 3 - ruby-on-rails

So, I have a very simple bit of code that works great in Rails 2, but breaks in Rails 3.
In Rails 2 I have the following for displaying page contents using a viewer controller from the Page model:
class ViewerController < ApplicationController
show
#page = Page.find_by_name(params[:name])
end
My viewer show view has the following:
<%= #page.body %>
My routes.rb file has the following to handle this action:
map.view_page ":name", :controller => 'viewer', :action => 'show'
Here is the error I get in Rails 3 using this code:
undefined method `body' for nil:NilClass
Now, I know the routes have to be changed in Rails 3, but what else am I missing to make this simple code work in a Rails 3 app? I can't seem to find the answer anywhere. Thanks!

match ":name", :to => "viewer#show", :as => "view_page"
Not tested not sure if it's match "/:name" or if ":name" is ok too.

Ok, Rails 3 have got new routing sintax
get "/:name" => 'viewer#show'
Useful links:
http://railscasts.com/episodes/203-routing-in-rails-3
http://guides.rubyonrails.org/routing.html
http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/
UPD
Your error is because Rails can't find any Page with your :name:
There is no any Page with this name
There is no any :name at all in your params hash

Make sure your database has the body field filled with something and it should work just like you have it.
domain.com/viewer/show?name=home

Related

Ruby on Rails 'link_to' method creates wrong link

I am having issues with the link_to method in Rails. I have the routes established, but the urls aren't working correctly.
3000/gov_official => my root page
3000/gov_official/1 => desired show page url
3000/gov_official.1 => what I am getting...
Any help would be greatly appreciated.
My code snippet:
Try the following in your routes instead of manually defining the index/show routes.
resource: :gov_official, only: [:get]
Probably having an issue link_to, try this one.
<%= link_to gov.name, gov_official_path(id: gov.id) %>
From my own experience, the problem is that Rails can't figure out which path are you trying to link to. Because, your resource is not in plural form gov_officials, you don't have 2 clearly separate paths:
gov_officials_path - takes 1 argument format. Generates: gov_officials.format.
gov_official_path - takes 1 argument model. Generates: gov_officials/:id
So, to solve your problem the Rails way, use pluralization of your resources correctly.

Rails route not working

I created a new route and a corresponding method in a controller and view:
routes.rb:
get "pictures/user/:username" => 'Pictures#user'
pictures_controller.rb:
def user
#pictures_user = User.where(username: params[:username]).first
render 'user.html.erb'
end
user.html.erb:
Just some simple html/erb
I get this following error. I know the user method is being entered, seems to be a problem rendering the view?
Routing Error
No route matches {:controller=>"pictures", :action=>"user", :username=>"my_username", :page=>nil}
What am I missing?
user.html.erb is inside the same directory of views/pictures? just do
render 'user'
should be fine i think
and I guess your Pictures downcase will be better ##
get "pictures/user/:username" => 'pictures#user'

autogenerate paths in rails 3?

From the looks of some railscasts (this one in particular), it seems like there is some autogeneration of "*_path" variables that not happening for me. in this rails cast, the edit_mutliple_products_path seems to be generated automagically (i don't normally like using that word). When I follow through the same steps and try to access a similar path, i get this:
undefined local variable or method `edit_multiple_distributions_workflows_path' for #<#<Class:0x132b18a68>:0x132af3290>
This is rails 2.X. Rails routes changed in Rails 3. in order to get this route add below to routes.rb:
resources :products do
collection do
post 'edit_multiple'
put 'update_multiple'
end
end
You will be able to access this paths with
edit_multiple_products_url
edit_multiple_products_path
update_multiple_products_url
update_multiple_products_path
instead of edit_multiple_distributions_workflow_path. Btw where did you get this path from? I did not see it in the railscast.
In the given tutorial, which looks like it's from an older Rails, this is the line which would generate the path methods:
map.resources :products, :collection => { :edit_multiple => :post, :update_multiple => :put }
In rails 3, you can see its usage in the docs here: http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default

quick rails named routes question

In my view I specify a named route like this:
show.html.erb ->
seo_path(:id => "45")
Now in my routes I define like so:
routes.rb ->
map.seo "/pages/:id/:permalink", :controller => "pages", :action => "show"
Below is the error message I am getting. Apparently the diff is the id, although I don't know why.
Update:
I am getting this as my error:
seo_url failed to generate from {:controller=>"pages", :id=>"45", :action=>"show"}, expected: {:controller=>"pages", :action=>"show"}, diff: {:id=>"45"}
Why not using the to_param method?
class YourModel << AR::Base
def to_param
"#{id}-#{title.parameterize}"
end
Thus getting routes like
/pages/1-here-is-my-converted-article-title-to-permalink
Besides that, it would seems Rails isn't recognizing your route. Have you restarted your web server? Try sending only a :permalink attribute to see how it goes.
I don't fully understand your question.
It looks like id is already specified in your named route, and in your view. It looks like you need to also specify permalink in your view. Are you getting an error?

Ruby on Rails Actions help

I have my index page which posts a single entry instead of the usual scaffold default of all of the entries. I told it to link to an action and it just responds to "Couldn't find Post with ID=all". It is the same as the default index method and index view. I assume this has something to do with routing but being no I have no clue. Any ideas?
The "all" name is misleading. If you want a page to display all the posts then the index page is perfect for that. If you want to show a subset of the posts then I recommend adding another action to your controller with a better name and then this to the routes.rb file:
map.resources :posts, :collection => { :some => :get }
Which you then can reference by using some_posts_path or some_posts_url.
For more information read the Official Ruby on Rails Routing guide.

Resources