Препратки и външни връзки
Ruby on Rails - Настройване на функцията за прегледи
NoMethodError in Discussions#new
http://ruby.about.com/od/rubyonrails/ss/blogpart4_4.htm
Фон
Внедрявам функция в моето приложение, която позволява на потребителите да оценяват и преглеждат снимки.
Използвам модел на връзка Публикации/Коментари за връзка Снимки/Отзиви.
Модели
class Review < ActiveRecord::Base
belongs_to :picture
end
class Picture < ActiveRecord::Base
has_many :reviews
end
По-горе установих връзка едно към много между снимки и рецензии.
Миграция на рецензии
class CreateReviews < ActiveRecord::Migration
def change
create_table :reviews do |t|
t.string :username
t.text :body
t.references :picture, index: true
t.timestamps
end
end
end
Съвпадащи маршрути
match '/pictures/:id/reviews', to: 'reviews#show', via: 'get', :as => 'picture_reviews'
match '/pictures/:id/reviews/edit', to: 'reviews#edit', via: 'get'
match '/pictures/:id/reviews/new', to: 'reviews#new', via: 'get', :as => 'new_reviews'
Ще назова маршрута за reviews#edit, след като поправя този проблем с reviews#new.
Съобщение за грешка
NoMethodError in Reviews#new
Undefined method 'reviews_path' for #<#<Class:0x45c1b00>:0x39ae810>
Extracted source (Around line #8):
5 <div class = 'edit-form'>
6 <div class = 'center'>
7
8 <% form_for @review do |f| %>
9
10 <p>
11 <%= f.label :username %><br />
Проверих дали някои файлове съдържат „review-path“, но всички маршрути бяха правилно именувани.
Маршрути
favorite_picture_path PUT /pictures/:id/favorite(.:format) pictures#favorite
pictures_path GET /pictures(.:format) pictures#index
POST /pictures(.:format) pictures#create
new_picture_path GET /pictures/new(.:format) pictures#new
edit_picture_path GET /pictures/:id/edit(.:format) pictures#edit
picture_path GET /pictures/:id(.:format) pictures#show
PATCH /pictures/:id(.:format) pictures#update
PUT /pictures/:id(.:format) pictures#update
DELETE /pictures/:id(.:format) pictures#destroy
users_path GET /users(.:format) users#index
POST /users(.:format) users#create
new_user_path GET /users/new(.:format) users#new
edit_user_path GET /users/:id/edit(.:format) users#edit
user_path GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions_path POST /sessions(.:format) sessions#create
new_session_path GET /sessions/new(.:format) sessions#new
session_path DELETE /sessions/:id(.:format) sessions#destroy
contacts_path POST /contacts(.:format) contacts#create
new_contact_path GET /contacts/new(.:format) contacts#new
root_path GET / pictures#welcome
users_new_path GET /users/new(.:format) users#new
about_path GET /about(.:format) pictures#about
GET /contacts(.:format) contacts#new
GET /users/:id/favorites(.:format) users#favorites
signup_path GET /signup(.:format) users#new
signin_path GET /signin(.:format) sessions#new
signout_path DELETE /signout(.:format) sessions#destroy
picture_reviews_path GET /pictures/:id/reviews(.:format) reviews#index
GET /pictures/:id/reviews/edit(.:format) reviews#edit
new_reviews_path GET /pictures/:id/reviews/new(.:format) reviews#new
updated_path GET /updated(.:format) pictures#new_updates
GET /top-rated(.:format) pictures#high_ratings
ReviewsController (Част 1)
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy]
def index
@picture = Picture.find(params[:id])
@review = Review.all
end
def show
@picture = Picture.find(params[:id])
@review = Review.find(params[:id])
end
def new
@review = Review.new
end
def edit
@picture = Picture.find(params[:picture_id])
@review = Review.find(params[:id])
end
def create
@picture = Picture.find(params[:picture_id])
@review = @picture.reviews.build(params[:review])
if @review.save
flash[:notice] = 'Review was successfully created.'
redirect_to @picture
else
flash[:notice] = "Error creating review: #{@review.errors}"
redirect_to @picture
end
end
Контролер за прегледи (част 2)
def update
@picture = Picture.find(params[:picture_id])
@review = Review.find(params[:id])
if @review.update_attributes(params[:review])
flash[:notice] = "Review updated"
redirect_to @picture
else
flash[:error] = "There was an error updating your review"
redirect_to @picture
end
end
def destroy
@picture = Picture.find(params[:picture_id])
@review = Review.find(params[:id])
@review.destroy
redirect_to(@review.post)
end
private
def set_review
@review = Review.find(params[:id])
end
def review_params
params.require(:review).permit(:username, :body, :picture_id)
end
end
Отзиви#Индексна страница
<h3>Reviews for <%= "#{@picture.title}" %></h3>
<table>
<thead>
</thead>
<tbody>
</tbody>
</table>
<div class = 'center'>
<p><%= link_to 'New Review', new_reviews_path(@review), :class => "btn btn-info" %></p>
<p><%= link_to 'Back', picture_path, :class => "btn btn-info" %></p>
</div>
Връзка към страница Ревюта#нова
<p><%= link_to 'New Review', new_reviews_path(@review), :class => "btn btn-info" %></p>
Отзиви#нова страница
<% @title = "New Review" %>
<h3>New Review</h3>
<div class = 'edit-form'>
<div class = 'center'>
<% form_for @review do |f| %>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Submit Review" %>
</p>
<% end %>
</div>
</div>
<div class = 'center'>
<%= link_to 'Back', picture_reviews_path(@picture) %>
</div>
Снимки#Покажи страница
<% @title = "#{@picture.title}" %>
<h4 class = 'indent'>Picture Statistics</h4>
<ul id = 'view'>
<li><strong>Title:</strong> <%= @picture.title %></li>
<li><strong>Category:</strong> <%= @picture.category %></li>
<li><strong>Rating:</strong> <%= pluralize(@picture.rating, 'Star') %></li>
<li><strong>Favorited:</strong> By <%= pluralize(@picture.users.count, 'User') %></li></br>
</ul>
<% if @picture.rating > 4 %>
<button class = 'top-picture'>Top Rated</button>
<% end %>
<%= form_for @picture do |f| %>
<div class = 'indent'>
<p>
<%= f.label :stars, 'Rating' %>
<div class= "rating">
1 ☆<%= f.radio_button :stars, '1' %>
2 ☆<%= f.radio_button :stars, '2' %>
3 ☆<%= f.radio_button :stars, '3' %>
4 ☆<%= f.radio_button :stars, '4' %>
5 ☆<%= f.radio_button :stars, '5' %>
</div>
</p>
<p><input class="btn btn-info" type="submit" value="Rate"></p>
<p><%= link_to 'Reviews', picture_reviews_path(@picture), :class => "btn btn-info" %></p>
<% end %>
<p><%= link_to 'Index', pictures_path, :class => "btn btn-info" %></p>
</div>
Опитах се да използвам вложени ресурси по този начин
resources :pictures do
put :favorite, on: :member
resources :reviews
end
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :contacts, only: [:new, :create]
Това не проработи, защото насочваше снимките ми, използвайки :picture_id вместо стандартното поле :id. Тъй като се насочи към :picture_id, не можа да намери никакви снимки.
picture_reviews_path GET /pictures/:picture_id/reviews(.:format) reviews#index
GET /pictures/:picture_id/reviews/edit/:id(.:format) reviews#edit
new_reviews_path GET /pictures/:picture_id/reviews/new(.:format) reviews#new
Колони с картини
Picture.column_names
=> ['id', 'title', 'category', 'stars', 'created_at', 'updated_at',
'ratings_count', 'ratings_total']
Проблемът с вложените маршрути е, че той извиква път, използвайки име на колона, което не е намерено в таблицата. Ето защо реших да се върна към съвпадащи маршрути.
Вярвам, че проблемът е в моя ReviewsController, за който може да има дублиран код.
before_action :set_review, only: [:show, :edit, :update, :destroy]
@review = Review.find(params[:id])
def set_review
@review = Review.find(params[:id])
end
Мисля, че бих могъл да премахна реда @review = Review.find от всеки метод, но основната ми грижа е, че методът set_review беше дефиниран като частен метод, така че може да не бъде възможно.
Помощта е много ценена и благодаря предварително.
Актуализация
Мисля, че проблемът се крие в моето ново действие в моя ReviewsController.