Links to resources with friendly URLs - ruby-on-rails-3

I'm trying to write a blog in Rails 3, so I have posts. I want to make nice routes for the posts like this: posts/year/month/day/post-title. So I overrided to_param in model/post.rb and use friendly_id for title:
extend FriendlyId
friendly_id :title, :use => :slugged
def to_param
"#{year}/#{month}/#{day}/#{title.parameterize}"
end
And I added this to routes.rb:
resources :posts do
collection do
match ":year/:month/:day/:id", :action => "show"
end
member do
post 'comment'
delete 'comment/:comment_id', :action => 'destroy_comment', :as => 'destroy_comment'
end
end
In show this works:
<%= link_to image_tag("/images/edit_icon.png"),
{ :controller => 'posts', :action => 'edit' }, :id => #post %>
But in index it says
routing error:
No route matches {:action=>"edit", :controller=>"posts"}.
I'm new to Rails and I haven't managed to find out what causes this error. Can anyone help me figure out what I do wrong?

Related

params of the model not being returned rails 3

I have made a few posts before this regarding how to add a favourite recipe to a user..I have an app where you can upload recipes once logged in, users can search the entire table for all recipes and view their own recipes in a member area..
Now I want users to be able to save their favourite recipes, so far I can save a favourite recipe as such, the output that I get is
[#<Favourite id: 1, user_id: 8, recipe_id: nil, created_at: "2012-11-06 19:25:34", updated_at: "2012-11-06 19:25:34">,
so i am getting the correct user_id but no params for the actual recipe, ie dish name, country of origin.
My models are like so
User
class User < ActiveRecord::Base
has_many :recipes
has_many :favourites
Recipe
has_many :ingredients
has_many :preperations
has_many :favourites
Favourite
belongs_to :user
belongs_to :recipe
My favourite controller looks like so
def create
#favourite = current_user.favourites.new(params[:recipe])
if #favourite.save
redirect_to my_recipes_path, :notice => "Recipe added to Favourites"
end
end
Add to favourites link
<%= link_to "Add to favorites", {:controller => 'favourites', :action => 'create'}, {:method => :post } %>
I hope I haven’t missed anything out, any help appreciated
You need to add extra information in the link and modify the create action
# View
<%= link_to "Add to favorites", favorite_path(:recipe_id => #recipe.id), {:method => :post } %>
# Controller
def create
#favourite = current_user.favourites.new(recipe_id: params[:recipe_id)
if #favourite.save
redirect_to my_recipes_path, :notice => "Recipe added to Favourites"
end
end
The problem is you are sending nothing to the controller in the param params[:recipe]
NOTE: remember the attr_accessible :user_id, :recipe_id inside Favorite model.
as stated
<%= link_to "Add to favorites", favorite_path(:recipe_id => #recipe.id), {:method => :post } %>
BUT this all depends on what #recipe is defined as in your controller - for example, if you have
#recipes = Recipie.all
And in the view you have
#recipes.all do |recipe|
Then in your link (within the block) you need to have:
<%= link_to "Add to favorites", favorite_path(:recipe_id => recipe.id), {:method => :post } %>
Does that help?
You're not sending any parameters through in the link.
<%= link_to "Add to favorites", {:controller => 'favourites', :action => 'create'}, {:method => :post } %>
This isn't enough to add a recipe to the favourites. What you'll need to do is pass through a recipe's id along with this link:
<%= link_to "Add to favorites", {:controller => 'favourites', :action => 'create', :recipe_id => recipe.id}, {:method => :post } %>
Or you could make this much shorter by using a routing helper:
<%= link_to "Add to favorites", add_to_favorites_path(:recipe_id => recipe), {:method => :post } %>
Defining that routing helper inside your config/routes.rb like this:
post '/favorites' => "favorites#create", :as => "add_to_favorites"
Then just find the recipe with params[:recipe_id] inside the controller and do what you need to do with it.

link_to different behavior from rails2 to rails3

In rails2, I was able to have code like this:
link_to(user.company.name, user.company)
which would map to:
/companies/id
but in rails 3, this same line of code throws a error stating:
undefined method `user_companies_path'
The obvious fix is to do something like:
link_to(user.company.name, company_path(user.company))
But I was wondering if anyone could explain the reason behind the change? The logic seemed a lot cleaner.
EDIT: Adding samples of my routes
In rails2, my routes looked like:
map.resources :users, :except => :edit, :member => { :details => :get }
map.resources :companies, :except => :edit, :member => { :details => :get }
In rails3, my routes are:
resources :users, :except => :edit do
member do
get :details
end
end
resources :companies, :except => :edit do
member do
get :details
end
end
The short answer is that the Rails 3 routing API bases your application on resources which is why these RESTful routes are being used, and also means that it does things like support constraints.
In Rails 2, you'd do:
resources :cars do
resource :models
member do
post :year
end
collection do
get :details
end
end
In Rails 3, you'd do:
map.resources :cars, :member => {:year => :post}, :collection => {:details => :get} do |cars|
cars.resource :model
end
You also have the :as key available which means you can then use named route helpers anywhere that url_for is available (i.e. controllers, mailers etc.)

Rails 3: Problem with a route to new action

I have a problem with rails 3 routes.
I want add a new action called "gestion_etudiant" with a new view "gestion_etudiant.html.erb".
I have on my index page a link like this
<%= link_to "Administration", {:controller => "users", :action => "gestion_etudiant"} %>
I also try this:
<%= link_to "Administration", "/users/gestion_etudiant" %>
In my controller:
def gestion_etudiant
#users = User.find(:all)
end
but when I clic on the link, I always have this error:
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with ID=gestion_etudiant
I have this in my routes file:
resources :users
And I've also try to add:
match "users/gestion_etudiant", :to => "users#gestion_etudiant"
and
resources :users, :only => [:gestion_etudiant]
But I can not access my page "gestion_etudiant.html.erb". Can anybody suggest why?
Try this:
# router:
resources :users do
get :gestion_etudiant, :on => :collection
end
# view:
link_to "Administration", gestion_etudiant_users_path
# Controller
def gestion_etudiant
#users = User.all # Don't use find with :all as it will be deprecated in rails 3.1
end
In your routes, try:
resources :users do
collection do
get 'gestion_etudiant'
end
end
You can check the routes you have in your application by running rake routes

Rails Routing Error

Rails is giving me a route error even though the route appears to
be in the route list.
The form is doing a Post to try to hit the update route on the Admin::ProductsController.
The edit route, index route, and show route work fine.
Using Rails 3.0.5 and ruby 1.9.2
Anyone have an idea? I can't seem to see the problem.
Error
No route matches "/admin/products/2039"
Code from ERB File that is generating the form
<%= form_for :product, #product, :url => { :action => "update" } do |f| %>
Products Controller method at this point is just a stub of
def update
puts params.inspect
end
Routes File
Analytics::Application.routes.draw do
match 'login' => 'Authentication#login', :via => [:get, :post]
namespace :admin do
# Directs /admin/products/* to Admin::ProductsController
# (app/controllers/admin/products_controller.rb)
root :to => 'AdminInterface#index', :via => :get
resources :products
resources :publishers, :only => [:edit, :update]
match 'publishers/query/:subset' => 'Publishers#index', :as => :publishers_subset, :via => [:get, :post]
end
end
According to your routes, shouldn't that be
= form_for [:admin, #product] do |f|
Your form_for can just be:
<%= form_for #product do |f| %>
If #product is an existing object then it will automatically know to go the update action of the ProductsController.

Will paginate in rails3

<%= will_paginate #semails, :renderer => 'RemoteLinkRenderer' , :remote => {
:loading => 'loadingPanel.show()',:complete => 'loadingPanel.hide()'} %>
in rails2
how to convert this to rails3
This is the routes using for rails 2 for semails
map.resources :users,
:collection => {:uapload_avatars => :post, :aselect_friend => :get, :alist_friend => :get, :aist_moderator => :get},
:member => {:anew_avatars => :get, :acreate_avatar => :post
} do |user|
user.resources :semails, :collection => { :sort => :get, :asave_draft => :post }
end
how to convert this routes in rails 3 ?
I'm facing this error
will clicking the pagination link it just redirecting to the home page is that routes issue or will paginate issue in rails 3
please help me to solve this issue
I'm not 100% about the spelling but the new routes for rails would approximate something like this:
resources :user do
collection do
post "uapload_avatars"
get "aselect_friend"
end
resources :semails do
collection do
get "sort"
post "asave_draft"
end
end
end
http://guides.rubyonrails.org/routing.html