你好,我是新来的导轨4 我工作的项目,我们可以发布和评分。我已经使用ActiveRecord的声誉宝石。我一直在关注 railscasts/episodes/364-active-创纪录的信誉系统的视频。 我停留在路由点。当我点击upvote或downvote它表明我的错误。
Hello I'm new to rails 4 I am working on project where we can post and rate it. I have used activerecord reputation gem. I have been following the railscasts/episodes/364-active-record-reputation-system video. I am stuck at the routing point. When I click on upvote or downvote it shows me error.
我routes.rb中文件有
my routes.rb file has
resources :posts do resources :comments, :only => [:create] member { post :vote } end get "posts/create" get "posts/destroy" get "posts/new" get "posts/index" get "posts/show" get "posts/edit" get "posts/update" _post.html.erb <h4 class="timeline-title"><%= link_to_unless_current post.title, post %></h4> <p> Created : <%= post.created_at.strftime("%d/%m/%Y") %> by <%= post.postedby %> </p> <% if current_cubestudent.email == post.postedby %> <p><%= link_to 'Edit', edit_post_path(@post) %> <%= link_to 'Destroy', posts_path, method: :delete, data: { confirm: 'Are you sure?' } %> <% end %> <%= simple_format post.body , :class => "timeline-body"%> <%= pluralize post.reputation_for( :votes).to_i, "vote" %> <% if current_cubestudent && !current_cubestudent.voted_for?(post) %> <%= link_to "up", vote_post_path(post, type: "up"), method: "post" %> <%= link_to "down", vote_post_path(post, type: "down"), method: "post" %> <% end %>
我的帖子#显示的是
my post#show is
<%= stylesheet_link_tag "application",:media =>"screen" %> <p id="notice"><%= notice %></p> <p id="alert"><%= alert %></p> <%= render :partial => @post %> <%= link_to 'Back', posts_path %> <h2 id="timeline">Comments</h2> <div> <%= render :partial => @postments %> </div> <div class="container"> <%= form_for [@post, Comment.new] do |f| %> <div style="padding-bottom:0"> <%= f.text_area :body , :placeholder => "Write your comment"%> <%= f.submit "Add comment", :class => "btn btn-info" %> </div> <% end %> </div>
岗位控制器
class PostsController < ApplicationController before_action :set_post, only: [:show, :edit, :update, :destroy] def create @post = Post.new(post_params) @post.postedby = current_cubestudent.email @post.postedbyid = current_cubestudent.id respond_to do |format| if @post.save format.html { redirect_to @post, notice: 'Post was successfully created.' } format.json { render action: 'show', status: :created, location: @post } else format.html { render action: 'new' } format.json { render json: @post.errors, status: :unprocessable_entity } end end end def destroy @post.destroy respond_to do |format| format.html { redirect_to posts_url } format.json { head :no_content } end end def new @post = Post.new end def index @posts = Post.find_with_reputation(:votes, :all, order: "votes desc") end def show @post = Post.find(params[:id]) end def edit end def update respond_to do |format| if @post.update(post_params) format.html { redirect_to @post, notice: 'Post was successfully updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @post.errors, status: :unprocessable_entity } end end end def vote value = params[:type] == "up" ? 1 : -1 @post = Post.find(params[:id]) @post.add_or_update_evaluation(:votes, value, current_cubestudent) redirect_to :back, notice: "Thank you for voting" end private # Use callbacks to share common setup or constraints between actions. def set_post @post = Post.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def post_params params.require(:post).permit(:title, :body, :subject) end end
后耙路线| grep的交
after rake routes | grep post
C:\网站\工程&GT;耙路线| grep的帖子 post_comments POST /posts/:post_id/comments(.:form评论#创建 vote_post POST /posts/:id/vote(.:format)的帖子#投票 帖子GET /posts(.:format)的帖子#指数 POST /posts(.:format)的帖子#创建 new_post GET /posts/new(.:format)的帖子#新 edit_post GET /posts/:id/edit(.:format)的帖子#编辑 后GET /posts/:id(.:format)的帖子#秀 PATCH /posts/:id(.:format)的帖子#更新 PUT /posts/:id(.:format)的帖子#更新 DELETE /posts/:id(.:format)的帖子#销毁 posts_create GET /posts/create(.:format)的帖子#创建 posts_destroy GET /posts/destroy(.:format)的帖子#销毁 posts_new GET /posts/new(.:format)的帖子#新 posts_index GET /posts/index(.:format)的帖子#指数 posts_show GET /posts/show(.:format)的帖子#秀 posts_edit GET /posts/edit(.:format)的帖子#编辑 posts_update GET /posts/update(.:format)的帖子#更新
C:\Sites\project>rake routes | grep post post_comments POST /posts/:post_id/comments(.:form comments#create vote_post POST /posts/:id/vote(.:format) posts#vote posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy posts_create GET /posts/create(.:format) posts#create posts_destroy GET /posts/destroy(.:format) posts#destroy posts_new GET /posts/new(.:format) posts#new posts_index GET /posts/index(.:format) posts#index posts_show GET /posts/show(.:format) posts#show posts_edit GET /posts/edit(.:format) posts#edit posts_update GET /posts/update(.:format) posts#update
有人可以帮助我就可以了...... ??
Can someone help me on it....??
推荐答案您的主要问题是在这个code
Your main problem is in this code
<%= link_to "up", vote_post_path(post, type: "up"), method: "post" %>您想送岗位的要求,但实际上您发送默认(得)的要求。 我可以假设 的link_to 接受只有符号方法(你传递字符串)。因此,尝试这种
you would like to send "post" request but in fact you send default ("get") request. I can suppose that link_to accepts only symbols for method (you pass string). So try this
<%= link_to "up", vote_post_path(post, type: "up"), method: :post %>
这个问题的另一个原因可能是错误的路线。如果code以上不帮你在控制台运行耙路线| grep的交并增加产量的问题。
Another source of the problem can be wrong routes. If code above does not help you run in console rake routes | grep post and add output to the question.
更多推荐
在轨道4路由错误的ActiveRecord的声誉宝石
发布评论