Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 当前用户的authlogic和POST_Ruby On Rails_Ruby_Authlogic - Fatal编程技术网

Ruby on rails 当前用户的authlogic和POST

Ruby on rails 当前用户的authlogic和POST,ruby-on-rails,ruby,authlogic,Ruby On Rails,Ruby,Authlogic,我使用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 <

我使用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 %>
posts控制器
def创建
@user=user.find(参数[:user\u id])
@post=@user.posts.create(参数[:post])
重定向到当前用户路径(@current\u user)
终止
显示页面
“你确定吗?”,:method=>:delete%>

添加一个帖子:

我知道它与当前用户有关,但我无法让它工作!非常感谢您的帮助

首先,您应该使用
current\u user
而不是
@current\u user


其次,您正在执行
@user=user.find(params[:user\u id])
,但我不明白为什么您希望在参数中包含类似
:user\u id
的内容。这就是为什么会出现“找不到没有ID的用户”错误。

通常,authlogic使用“当前用户”而不是“@current\u用户”。。。是否自定义为实例变量(而不是方法调用)?当前用户返回错误。看起来它是路由到my posts模型,而不是使用url中的用户id