【问题标题】:authlogic and posts for current_usercurrent_user 的 authlogic 和帖子
【发布时间】:2011-08-17 16:35:57
【问题描述】:

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

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

我知道这与当前用户有关,但我无法使其正常工作!非常感谢任何帮助!

【问题讨论】:

  • 通常,authlogic 使用“current_user”而不是“@current_user”...您是否自定义为实例变量(而不是方法调用)?
  • current_user 返回错误。看起来它正在路由到我的帖子模型,而不是在 url 中使用用户 ID

标签: ruby-on-rails ruby authlogic


【解决方案1】:

首先,您应该使用current_user 而不是@current_user

其次,你正在做@user = User.find(params[:user_id]),但我不明白你为什么希望在你的参数中有类似:user_id 的东西。这就是您收到“找不到没有 ID 的用户”错误的原因。

【讨论】:

    猜你喜欢
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多