Ruby on Rails Monday, December 30, 2013

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)


<h1>Listing movies</h1>

<table>
<thead>
<tr>
<th>Cover</th>
<th>Name</th>
<th>Description</th>
<th>Rented</th>
<th>Genre</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>

<tbody>
<% @movies.each do |movie| %>
<tr>
<td><img src="<%= movie.cover %>" width="95"/></td>
<td><%= movie.name %></td>
<td><%= movie.description %></td>
<td><%= movie.rented %></td>
<td><%= movie.genre %></td>
<td><%= link_to 'Rent', action: :rent, :id => movie.id %></td>
# the link that I told
<td><%= link_to 'Show', movie %></td>
<td><%= link_to 'Edit', edit_movie_path(movie) %></td>
<td><%= link_to 'Destroy', movie, method: :delete, data: {
confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= 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"}

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9eb5c007084d1ca7bf1ac411467a4344%40ruby-forum.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment