2016-03-04 48 views
0

我成立了一個Rails玩具與Twitter API來播放,但已經有從一開始就挑戰。我的文件是這樣的:的ActionController :: UrlGenerationError路由錯誤

的routes.rb

Rails.application.routes.draw do 

    devise_for :users 
    root to: 'accounts#index' 

    resources :accounts do 
    resources :posts 
    end 
end 

account.rb

class Account < ActiveRecord::Base 
    belongs_to :user 
    has_many :posts 

    accepts_nested_attributes_for :posts 

    validates :name, :description, presence: :true 
end 

post.rb

class Post < ActiveRecord::Base 
    belongs_to :account 

    validates :tweet, presence: true 
end 

accounts_controller.rb

class AccountsController < ApplicationController 
    before_action :authenticate_user! 

    def index 
    @accounts = Account.all 
    @user = current_user 
    end 

    def new 
    @account = Account.new 
    end 

    def create 
    @account = Account.new(account_params) 
    @account.user = current_user 
    if @account.save 
     flash[:notice] = "Account succesfully created." 
     redirect_to @account 
    else 
     flash.now[:alert] = "Oops, something went wrong!" 
     render 'new' 
    end 
    end 

    def show 
    @account = Account.find(params[:id]) 
    end 

    def update 
    end 

    def destroy 
    end 

    private 
    def account_params 
    params.require(:account).permit(:name, :description, posts_attributes: [:tweet]) 
    end 
end 

post_controller.rb

class PostsController < ApplicationController 
    before_action :set_account 


    def index 
    @posts = Post.all 
    end 

    def new 
    @post = Post.new 
    end 

    def create 
    @post = @account.posts.build(post_params) 
    if @post.save 
     flash[:notice] = "Tweet created successfully." 
     redirect_to [@account, @post] 
    else 
     flash.now[:alert] = "Something went wrong." 
     render 'new' 
    end 
    end 

    def edit 
    @post = Post.find(params[:id]) 
    end 
    def update 
    if @post.update 
     flash[:notice] = "Tweet updated." 
     redirect_to [@account, @post] 
    else 
     flash.now[:alert] = "Something is not right!" 
     render 'edit' 
    end 
    end 

    def destroy 
    end 

    def show 
    @post = Post.find(params[:id]) 
    end 

    private 
    def post_params 
    params.require(:post).permit(:tweet) 
    end 

    def set_account 
    @account = Account.find(params[:account_id]) 
    end 
end 

棘手的部分是在這裏: 我想在這裏做的是賬戶頁面上,當帳戶名稱的用戶點擊,他應該是重定向到的new行動該控制器將允許他爲有問題的帳戶創建新帖子。不知何故,我不知道如何通過:account_id參數。

的意見/帳號/ index.html.erb

<table> 
    <tr> 
     <th>Account Name</th> 
     <th>Description</th> 
     <th>Tweets</th> 
    </tr> 

    <% @user.accounts.each do |account| %> 
     <tr> 
      <td><%= link_to account.name, new_account_post_path(@account) %></td> 
      <td><%= account.description %></td> 
      <td><%= account.posts.count %></td> 

     </tr> 
    <% end %> 
</table> 


<%= link_to "Sign out", destroy_user_session_path, :method => :delete %> 
<%= link_to "Create new account", new_account_path %> 

錯誤的瀏覽器: enter image description here

回答

1

您需要更改

<%= link_to account.name, new_account_post_path(@account) %> 

<%= link_to account.name, new_account_post_path(account) %> 
+0

這個工程。但我需要了解爲什麼'帳戶'而不是'@帳戶'。我在想,如果我使用'@帳戶',我會得到有問題的帳戶的':account_id'。 – Emanuel

+1

'account'來自'@user.accounts.each do | account |'行。沒有定義「@帳戶」。 – Babar

+0

現在對我來說很有意義。謝謝@Babar – Emanuel

-2

使用該代碼:

<% @user.accounts.each do |account| %> 
    <tr> 
     <td><%= link_to account.name, new_account_post_path(account) %></td> 
     <td><%= account.description %></td> 
     <td><%= account.posts.count %></td> 

    </tr> 
<% end %> 
相關問題