2016-03-04 49 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 %> 
相关问题