I’m programming a web system for a rental of movies and put the resource
for rent a movie, but the routes don’t maches and the rails gives me
that message of error. My code and the error gived by rails are in
pictures. Thank you for the attention and for the help.
On 30 December 2013 00:39, Joo P. [email protected] wrote:
I’m programming a web system for a rental of movies and put the resource
for rent a movie, but the routes don’t maches and the rails gives me
that message of error. My code and the error gived by rails are in
pictures. Thank you for the attention and for the help.
It is better to copy/paste information rather than linking to images.
That way we can insert comments inline in the text. Only post
information that is relevant to the problem of course.
Even more significantly you have not posted the contents of routes.rb
or shown us the output from
rake routes
Colin
On 30 December 2013 15:06, Joo P. [email protected] wrote:
Sorry Colin, I’ll try to explain in a better way. In my system I have a
variable called ‘movie’ and this ‘movie’ have a boolean attribute
‘rented’ that describes if this ‘movie’ was rented or not. I’ve created
a scaffold to this variable and int this scaffold I put a new link to
modify this attribute in this way:
Please remember to quote the previous message and insert your reply at
appropriate points.
…
<%= link_to ‘Rent’, action: :rent, :id => movie.id %> the link that I told
…
You can have the root of your site routed with “root”
automatically):
get ‘sold’
resources :products do
resources :posts, concerns: :toggleable
------------------------------------------> error in browser
No route matches {:action=>“rent”, :id=>1, :controller=>“movies”}
You have asked for action rent on the movies controller but you have
not provided a route for that.
I suggest you work right through a good tutorial such as
railstutorial.org (which is free to use online) which will show you
the basics of rails.
If you have a look at the Rails Guide on Routing then it will show you
how to do it, but best to work through the tutorial first.
Colin
Sorry Colin, I’ll try to explain in a better way. In my system I have a
variable called ‘movie’ and this ‘movie’ have a boolean attribute
‘rented’ that describes if this ‘movie’ was rented or not. I’ve created
a scaffold to this variable and int this scaffold I put a new link to
modify this attribute in this way:
------------------------------------------> index.html.erb (view of the
listing)
Listing movies
Cover | Name | Description | Rented | Genre | ||||
---|---|---|---|---|---|---|---|---|
<%= movie.name %> | <%= movie.description %> | <%= movie.rented %> | <%= movie.genre %> | <%= link_to 'Rent', action: :rent, :id => movie.id %> | # the link that I told<%= link_to 'Show', movie %> | <%= link_to 'Edit', edit_movie_path(movie) %> | <%= link_to 'Destroy', movie, method: :delete, data: { confirm: 'Are you sure?' } %> |
<%= link_to ‘New Movie’, new_movie_path %>
------------------------------------------> movies_controller.rb
class MoviesController < ApplicationController
before_action :set_movie, only: [:show, :edit, :update, :destroy,
:rent]
def index
@movies = Movie.all
end
def show
end
def new
@movie = Movie.new
end
def edit
end
def create
@movie = Movie.new(movie_params)
respond_to do |format|
if @movie.save
format.html { redirect_to @movie, notice: 'Movie was
successfully created.’ }
format.json { render action: ‘show’, status: :created, location:
@movie }
else
format.html { render action: ‘new’ }
format.json { render json: @movie.errors, status:
:unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @movie.update(movie_params)
format.html { redirect_to @movie, notice: ‘Movie was
successfully updated.’ }
format.json { head :no_content }
else
format.html { render action: ‘edit’ }
format.json { render json: @movie.errors, status:
:unprocessable_entity }
end
end
end
def destroy
@movie.destroy
respond_to do |format|
format.html { redirect_to movies_url }
format.json { head :no_content }
end
end
def rent
@movie.to_rent
end
private
def set_movie
@movie = Movie.find(params[:id])
end
def movie_params
params.require(:movie).permit(:name, :description, :cover,
:rented, :genre)
end
end
------------------------------------------> movie.rb
class Movie < ActiveRecord::Base
def to_rent
self.rented = true
end
end
------------------------------------------> routes.rb
Locadora::Application.routes.draw do
resources :movies
The priority is based upon order of creation: first created ->
highest priority.
See how all your routes lay out with “rake routes”.
You can have the root of your site routed with “root”
root ‘welcome#index’
Example of regular route:
get ‘products/:id’ => ‘catalog#view’
Example of named route that can be invoked with purchase_url(id:
product.id)
get ‘products/:id/purchase’ => ‘catalog#purchase’, as: :purchase
Example resource route (maps HTTP verbs to controller actions
automatically):
resources :products
Example resource route with options:
resources :products do
member do
get ‘short’
post ‘toggle’
end
collection do
get ‘sold’
end
end
Example resource route with sub-resources:
resources :products do
resources :comments, :sales
resource :seller
end
Example resource route with more complex sub-resources:
resources :products do
resources :comments
resources :sales do
get ‘recent’, on: :collection
end
end
Example resource route with concerns:
concern :toggleable do
post ‘toggle’
end
resources :posts, concerns: :toggleable
resources :photos, concerns: :toggleable
Example resource route within a namespace:
namespace :admin do
# Directs /admin/products/* to Admin::ProductsController
# (app/controllers/admin/products_controller.rb)
resources :products
end
end
------------------------------------------> error in browser
No route matches {:action=>“rent”, :id=>1, :controller=>“movies”}