视图实际上是呈现主题ID而不是主题名称。以下是主题索引视图的代码。
<h1>Topics</h1>
<div class="row">
<div class="span8">
<% @topics.each do |topic| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<%= link_to topic.name, topic %>
<h1>Test</h1>
</h4>
<small>
<%= topic.description %>
</small>
</div>
</div>
<% end %>
<%= will_paginate @topics %>
</div>
<div class="span4">
<%= link_to "New Topic", new_topic_path, class: 'btn btn-large btn-block' %>
</div>
</div>
<script>
我认为我不需要像friendly_id那样让名称正确呈现。我可能是错的..但是我无法用Friendly_Id解决问题。有人提到它可能是一个路由问题,所以这是我的路线。
的routes.rb
Bloccit::Application.routes.draw do
get "posts/index"
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks', registrations: 'users/registrations' }
resources :users, only: [:show, :index]
resources :posts, only: [:index]
resources :topics do
resources :posts, except: [:index], controller: 'topics/posts' do
resources :comments, only: [:create, :destroy]
match '/up-vote', to: 'votes#up_vote', as: :up_vote
match '/down-vote', to: 'votes#down_vote', as: :down_vote
resources :favorites, only: [:create, :destroy]
end
end
match "about" => 'welcome#about', via: :get
root to: 'welcome#index'
end
这是topic.rb
class Topic < ActiveRecord::Base
# has_friendly_id :name, :use_slug => true
attr_accessible :description, :name, :public, :topic
has_many :posts, dependent: :destroy
scope :visible_to, lambda { |user| user ? scoped : where(public: true) }
end
这是topics_controller.rb
class TopicsController < ApplicationController
def index
# @topics = Topic.all
@topics = Topic.visible_to(current_user).paginate(page: params[:page], per_page: 10)
end
def new
@topic = Topic.new
authorize! :create, @topic, message: "You need to be an admin to do that."
end
def show
@topic = Topic.find(params[:id])
authorize! :read, @topic, message: "You need to be signed in to do that."
@posts = @topic.posts.includes(:user).includes(:comments).paginate(page: params[:page], per_page: 10)
end
def edit
@topic = Topic.find(params[:id])
authorize! :update, @topic, message: "You need to be an admin to do that."
end
def create
@topic = Topic.new(params[:id])
authorize! :create, @topic, message: "You need to be an admin to do that."
if @topic.save
redirect_to @topic, notice: "Topic was saved successfully"
else
flash[:error] = "Error creating topic. Please try again."
render :new
end
end
def update
@topic = Topic.find(params[:id])
authorize! :update, @topic, message: "You need to own the topic to update it"
if @topic.update_attributes(params[:topic])
redirect_to @topic, notice: "Topic was saved successfully."
else
flash[:error] = "Error saving topic. Please try again."
render :edit
end
end
def destroy
@topic = Topic.find(params[:id])
name = @topic.name
authorize! :destroy, @topic, message: "You need to own the topic to delete it."
if @topic.destroy
flash[:notice] = "\"#{name}\" was deleted successfully."
redirect_to topics_path
else
flash[:error] = "There was an error deleting the topic."
render :show
end
end
end
我不知道这是否有用,但我有一个嵌套在主题下的帖子控制器。
主题/ posts_controller.rb
class Topics::PostsController < ApplicationController
def show
@topic = Topic.find(params[:topic_id])
authorize! :read, @topic, message: "You need to be user to do that."
@post = Post.find(params[:id])
@comments = @post.comments
@comment = Comment.new
end
def new
@topic = Topic.find(params[:topic_id])
@post = Post.new
authorize! :create, Post, message: "You need to be a member to share a new source."
end
def edit
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
authorize! :edit, @post, message: "You need to own the post to edit it."
end
def create
@topic = Topic.find(params[:topic_id])
@post = current_user.posts.build(params[:post])
@post.topic = @topic
authorize! :create, @post, message: "You need to be signed up to do that."
if @post.save
flash[:notice] = "Post was saved."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :new
end
end
def update
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
authorize! :update, @post, message: "You need to own the post to edit it."
if @post.update_attributes(params[:post])
flash[:notice] = "Post was updated."
redirect_to [@topic, @post]
else
flash[:error] = "There was an error saving the post. Please try again."
render :edit
end
end
def destroy
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
@comments = @post.comments
@comment = Comment.new
title = @post.title
authorize! :destroy, @post, message: "You need to own the post to delete it."
if @post.destroy
flash[:notice] = "\"#{title}\" was deleted successfully."
redirect_to @topic
else
flash[:error] = "There was an error deleting the post."
render :show
end
end
end
答案 0 :(得分:1)
在create
中的topics_controller.rb
操作中,您使用的params[:id]
应该是params[:topic]
:
def create
@topic = Topic.new(params[:topic])
authorize! :create, @topic, message: "You need to be an admin to do that."
if @topic.save
redirect_to @topic, notice: "Topic was saved successfully"
else
flash[:error] = "Error creating topic. Please try again."
render :new
end
end