ActionController :: UrlGenerationError路由错误

时间:2016-03-04 12:56:25

标签: ruby-on-rails ruby routes

我设置了一个Rails玩具来玩Twitter API,但从一开始就已经有了挑战。我的文件看起来像这样:

的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控制器的Posts操作,这将允许他创建一个新的发布相关帐户。不知何故,我不知道如何传递: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

2 个答案:

答案 0 :(得分:1)

您需要更改

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

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

答案 1 :(得分:-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 %>