Rails应用程序不会呈现主题名称,而是呈现主题/ 9
问题描述:
视图本质上是呈现主题ID而不是主题名称。以下是主题索引视图的代码。Rails应用程序不会呈现主题名称,而是呈现主题/ 9
<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
答
在topics_controller.rb
你create
操作,您可以使用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
是正确的名称出现?你需要的路线是“主题/名称”? – mohameddiaa27 2014-11-05 21:25:14
该名称显示为主题/ 1而不是主题名称。我不认为我不应该需要一个宝石才能正确显示它。我在上面添加了我的路线.. – fresh5447 2014-11-05 23:34:45
我的意思是你想要这个名字作为一个id,所以而不是主题/ id它成为话题/名称? – mohameddiaa27 2014-11-05 23:36:38