2014-11-05 72 views
1

視圖本質上是呈現主題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 
+0

是正確的名稱出現?你需要的路線是「主題/名稱」? – mohameddiaa27 2014-11-05 21:25:14

+0

該名稱顯示爲主題/ 1而不是主題名稱。我不認爲我不應該需要一個寶石才能正確顯示它。我在上面添加了我的路線.. – fresh5447 2014-11-05 23:34:45

+0

我的意思是你想要這個名字作爲一個id,所以而不是主題/ id它成爲話題/名稱? – mohameddiaa27 2014-11-05 23:36:38

回答

1

topics_controller.rbcreate操作,您可以使用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