2011-08-17 86 views
0

我使用authlogic为用户登录,一旦他们这样做,我希望他们能够添加帖子。出于某种原因,我不断收到“无法找到没有ID的用户”。下面是我的代码:authlogic和当前用户的帖子

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 %> 

我知道它与当前用户有关,但我无法得到它的工作!任何帮助是极大的赞赏!

+0

通常情况下,authlogic使用“current_user”而不是“@current_user”......您是否自定义为实例变量(而不是方法调用)? –

+0

current_user返回一个错误。它看起来像是路由到我的帖子模型,而不是在url中使用用户ID – user893447

回答

0

首先,你应该使用current_user@current_user

其次,你在做@user = User.find(params[:user_id])但我不明白你为什么希望有像在PARAMS :user_id。这就是为什么你得到“找不到没有ID的用户”错误。