开发者

authlogic and posts for current_user

开发者 https://www.devze.com 2023-03-29 06:42 出处:网络
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:

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号