2013-06-13 7 views
0

У меня есть приложение, в котором пользователь может создать продукт. На странице продукта у меня есть ссылка на редактирование и некоторый текст.Rails - Create Error

Когда я создаю новый продукт. Я получаю

Маршрутизация Ошибка Нет маршрута матчей {: действие => «редактировать»,: контроллер => «продукты»}

Однако, когда я проверяю свою базу данных продукт был создан, и я могу просмотрите страницу просто отлично. Включая кнопку редактирования.

страница продукта (Haml)

= @product.name 
    %br 
    = @product.description 
    %br 
    - if current_user.admin || @product.user_id == current_user.id 
    = link_to 'Edit', edit_product_path 
    = link_to 'Delete', @product, method: :delete, data: { confirm: "Are you sure?" } 

продукты Контроллер

class ProductsController < ApplicationController 
    before_filter :require_login 
    before_filter :current_user, only: [:create, :destory] 
    before_filter :admin_and_author, only: [:destory, :edit] 
    before_filter :admin_user, only: :index 

    def new 
    @product = Product.new 
    @photo = Photo.new 
    4.times{ @product.photos.build } 
    end 

    def create 
    @product = current_user.products.new(params[:product]) 
    @photo = current_user.photos.new(params[:photo]) 

    if @product.valid? 
     @product.save 
     @photo.product_id = @product.id 
     @photo.save 
     render "show", :notice => "Sale created!" 
    else 
     render "new", :notice => "Somehting went wrong!" 
    end 
end 

    def show 
    @product = Product.find(params[:id]) 
    end 

    def edit 
    @product = Product.find(params[:id]) 
    @photo = @product.photos 
    end 

private 

    def correct_user 
    @product = current_user.products.find_by_id(params[:id]) 
    redirect_to root_url if @product.nil? 
    end  

    def require_login 
    unless current_user 
     redirect_to root_url 
    end 
    end 

    def admin_and_author 
    @product = current_user.products.find_by_id(params[:id]) 
    redirect_to root_url if @product.nil? || current_user.admin 
    end 

def admin_user 
    redirect_to(root_path) unless current_user.admin? 
end 

end 

контроллер приложения

class ApplicationController < ActionController::Base 
    protect_from_forgery 
    helper_method :current_user 

private 
    def current_user 
    @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token] 
    end 

    def admin_and_user 
    if current_user.id == 1 || current_user.admin 
    else 
     redirect_to(root_path) 
    end 
    end 


end 

создания страницы продукта (Haml)

%h1 
    create item 
= form_for @product,:url => products_path, :html => { :multipart => true } do |f| 
    %p 
    = f.label :name 
    = f.text_field :name 
    %p 
    = f.label :description 
    = f.text_field :description 
    %p 
    = f.fields_for :photos do |fp| 
     =fp.file_field :image 
     %br 
    %p.button 
    = f.submit 

Rake маршруты

  products GET /products(.:format)     products#index 
        POST /products(.:format)     products#create 
     new_product GET /products/new(.:format)    products#new 
     edit_product GET /products/:id/edit(.:format)   products#edit 
      product GET /products/:id(.:format)    products#show 
        PUT /products/:id(.:format)    products#update 
        DELETE /products/:id(.:format)    products#destroy 

ответ

4

заменить эту строку на странице:

= link_to 'Edit', edit_product_path 

с

= link_to 'Edit', edit_product_path(@product) 

или

= link_to 'Edit', [:edit, @product] 
+0

Спасибо! это было правильно (я должен подождать еще 5 минут, прежде чем я это сделаю правильно) –

1

Вы должны render что-то в вашем методе edit.

+0

но при нажатии кнопки редактирования он делает ссылку на edit_product_path –

1

Любой edit или show Пути требуют ссылочного объекта. Взгляните на мой ответ на аналогичный question. Это должно помочь вам.