skip to Main Content

I set up a Rails toy to play with Twitter API, but already having challenges right from the start. My files look like this:

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

The tricky part is here:
What I am trying to do here is on the accounts page, when a user clicks on the account name, he should be redirects to the new action of the Posts controller that will allow him to create a new post for the account in question. Somehow, I am not sure how to pass the :account_id parameter.

views/accounts/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 %>

Error in browser:
enter image description here

2

Answers


  1. You need to change

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

    to

    <%= link_to account.name, new_account_post_path(account) %>
    
    Login or Signup to reply.
  2. Use this code:

    <% @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 %>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search