Ruby on Rails的 - Settting了评论功能 [英] Ruby on Rails - Settting up Reviews functionality

查看:134
本文介绍了Ruby on Rails的 - Settting了评论功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立在我的Ruby on Rails应用程序功能,可让用户查看照片。

I am trying to set up a feature on my Ruby on Rails app that lets users to review pictures.

我已经按照本指南作为参考。

I've followed this guide as a reference.

http://ruby.about.com/od/rubyonrails/ss/ blogpart4_4.htm

从我的经验对其他Ruby工作on Rails的项目,我觉得文章/评论关系模型在这里可以用于图片/点评关系。

From my experiences working on other Ruby on Rails projects, I think a Posts/Comments relationship model can be used here for a Pictures/Reviews relationship.

我第一次产生了脚手架。

I first generated a scaffold.

rails g scaffold review name:string body:text picture:references

我想每个图片页面有其自己的意见一个单独的页面。

I would like each picture page to have a separate page for its own reviews.

因为我并不需要一个索引页对我的评论的控制器,我删除了这条线从我的routes.rb文件

Since I don't need an Index Page for my reviews controller, I removed this line from my routes.rb file

resources: reviews

我取代,通过创建路由

I replaced that by creating routes

match '/pictures/:id/reviews', to: 'reviews#show', via: 'get'
match '/pictures/:id/reviews/edit', to: 'reviews#edit', via: 'get'
match '/pictures/:id/reviews/new', to: 'reviews#new', via: 'get'

在这里,我的道路涉及的图片里面嵌套评论。

Here, my path involves nesting reviews inside pictures.

路线

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
                        GET     /pictures/:id/reviews(.:format)     reviews#show
                        GET     /pictures/:id/reviews/edit(.:format)    reviews#edit
                        GET     /pictures/:id/reviews/new(.:format) reviews#new
updated_path            GET     /updated(.:format)              pictures#newest_updates
                        GET     /top-rated(.:format)            pictures#high_ratings

ReviewsController

class ReviewsController < ApplicationController
  before_action :set_review, only: [:show, :edit, :update, :destroy]

  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

  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

我从ReviewsController删除索引操作。

I deleted the index action from my ReviewsController.

模式

class Review < ActiveRecord::Base
  belongs_to :picture
end

class Picture < ActiveRecord::Base
  has_many :reviews
end

以上,我建立了图片和评论之间存在一个一对多的关系。

Above, I established a one-to-many relationship between pictures and reviews.

评论迁移

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

根据我的on Rails的理解,这应该工作。

Based on my understanding on Rails, this should work.

图片#显示页面

<% @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| %>

  <p>
    <%= f.label :stars, 'Rating', class: 'indent' %>
    <div class= "rating">
      1 &#9734;<%= f.radio_button :stars, '1' %>
      2 &#9734;<%= f.radio_button :stars, '2' %>
      3 &#9734;<%= f.radio_button :stars, '3' %>
      4 &#9734;<%= f.radio_button :stars, '4' %>
      5 &#9734;<%= f.radio_button :stars, '5' %>
    </div>
  </p>

  <p class = 'indent'>
   <input class="btn btn-info" type="submit" value="Review">
  </p>

  <a href = "/pictures/:id/reviews">Reviews</a>

<% end %>
<p class = 'indent'>
  <a class="btn btn-info" href="/pictures" role="button">Index</a>
</p>

然而,当我点击我的图片/链路上的:ID(显示页)

However, when I click on a link in my Pictures/:id(show page)

<a href = "/pictures/:id/reviews">Reviews</a>

RecordNotFound错误

Active Record::RecordNotFound in ReviewsController#show
Couldn't find Review with id=:id

Extracted source (around line #54):
53 def set_review
54  @review = Review.find(params[:id])
55 end
56
57 def review_params

因为我遇到了一个错误RecordNotFound,我怀疑问题出在ReviewsController,最有可能与PARAMS。

Since I encountered a RecordNotFound error, I suspect the problem lies in the ReviewsController, most likely with the params.

我相信我有正确的想法,但我什么地方弄错了。反馈和批评都大大AP preciated。很抱歉,如果这听起来像一个愚蠢的问题,我只是没有在Ruby中非常好。

I believe that I have the right idea, but I made a mistake somewhere. Feedback and criticism are greatly appreciated. Sorry if this sounds like a stupid question, I'm just not very good in Ruby.

推荐答案

路线

有关子孙后代着想,你最好设置你的路由,如下所示:

For the sake of posterity, you'll be best setting your routes up as follows:

#config/routes.rb
resources :pictures do
   resources :reviews, only: [:show, :edit, :new]
end

每当您创建Rails的路线,你需要记住的整个框架一直围绕打造的对象/资源。这就是为什么路由被称为足智多谋路线(为什么他们有资源指令) - 它们允许你定义路由围绕的应用程序的不同的资源

Whenever you create routes in Rails, you need to remember the entire framework has been built around "objects" / "resources". That's why the routes are known as resourceful routes (and why they have the resources directive) - they allow you to define the routes around the different resources of your application

我已经推荐使用嵌套在 资源结构。

I've recommend using a nested resources structure.

-

助手

您的问题是使用由桑托斯之类的提供的code解决。 IE浏览器:

Your problem was resolved using the code provided by Santosh and the like. IE:

<%= link_to "Your Link", your_link_path(@object) %>

您需要的AP preciate的如何的工作原理。每次使用路线帮助在Rails中(在的link_to 助手)的时候,它会去翻你的路由和放大器;发现它所需要的信息。

You need to appreciate how this works. Each time you use a route helper in Rails (in the link_to helper), it will look through your routes & find the details it needs.

您被引用以下路径:图片/:ID /条 - 因为发现了,这是错误的,因为Rails没有对链接的网址之外任何影响构建它的渲染时间。

You were referencing the following path: pictures/:id/reviews - as discovered, this is wrong because Rails does not have any bearing on the URL of a link besides building it at render time.

考虑Rails是一个无国籍基于HTTP的框架,Rails有整理在每次的任何数据请求。这意味着如果你想建立联系,你必须让Rails的建设在渲染环节,通过一组静态数据通过你的控制器后端

Considering Rails is a stateless HTTP-based framework, Rails has to collate any data upon each request. This means if you want to build links, you have to let Rails build the link at render, passing a static set of data through to your controller in the back-end

希望这有助于?

这篇关于Ruby on Rails的 - Settting了评论功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆