0
votes

I am using authlogic for users to login and once they do so, I would like them to be able to add posts. For some reason I keep getting "Couldn't find User without an ID". Below is my code:

posts controller
def create
  @user = User.find(params[:user_id])
  @post = @user.posts.create(params[:post])
  redirect_to current_user_path(@current_user)  
end

show page
  <% @user.posts.each do |post| %>
 <tr>
   <td><%= post.postName %></td>
   <td><%= post.body %></td>
   <td><%= link_to 'Show', post %></td>
   <td><%= link_to 'Edit', edit_post_path(post) %></td>
   <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>       </td>
 </tr>
<% end %>
</table>

<br />

<h2>Add a Post:</h2>
<%= form_for([@current_user, @user.posts.build]) do |f| %>

  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

I know it has something to do with current user, but I cannot get this working! Any help is greatly appreciated!

1
Normally, authlogic uses "current_user" not "@current_user" ... did you customize to be instance variable (rather than a method call)? - Jesse Wolgamott
current_user returns an error. It looks like it is routing to my posts model instead of using the user id in the url - user893447

1 Answers

0
votes

First, you should use current_user not @current_user

Second, you're doing @user = User.find(params[:user_id]) but I don't understand why you expect to have something like :user_id in your params. That's why you get that "Couldn't find User without an ID" error.