【问题标题】:RoR, params not being passed to the controller for a :remote link_toRoR,未将参数传递给控制器​​以获取:remote link_to
【发布时间】:2012-01-15 19:54:34
【问题描述】:

好吧,老实说,我没有花太多时间寻找解决方案,但看看我儿子如何让我的注意力四处奔走。无论哪种方式,我都想问一个看起来很简单但到目前为止让我难过的问题。

所以为了简单起见,假设我有用户(w/model)和 Home 控制器,Home 是根路由。

在根目录中,我希望能够看到用户使用 ajax 使用帖子列表更新主页上的部分帖子。

users 控制器中,我有一个名为 userposts 的定义,其中包含此内容

def userposts
  @user = User.find_by_id(params[:id])
  @userposts = @user.posts.all(:order => "created_at DESC")
  respond_to do |format|
    format.js { @userposts}
  end    
end

在我看来,我有

<p id="aboutuser">
  <% if @user.about? %>
    <%= "   " + @user.id.to_s %>
  <% else %>
    User has not yet filled this out.
  <% end %>
</p> 
<h3 id="authpostlink">
  <%= link_to "List of all posts", user_userposts_path(@user.id), :id => @user.id, :remote => true %>
</h3>

我的错误如下

于 1 月 15 日星期日 13:36:23 开始为 127.0.0.1 获取“/users/2/userposts” -0600 2012 由 UsersController#userposts 作为 JS 参数处理:{"user_id"=>"2"} 用户负载 (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1 Completed 500 Internal 1ms 内的服务器错误

NoMethodError(未定义方法posts' for nil:NilClass):
app/controllers/users_controller.rb:27:in
userposts'

渲染 /home/n0de/.rvm/gems/ree-1.8.7-2011.03/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.8ms) 渲染 /home/n0de/.rvm/gems/ree-1.8.7-2011.03/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms) 渲染 /home/n0de/.rvm/gems/ree-1.8.7-2011.03/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb 在救援/布局中(3.2 毫秒)

我确实意识到我没有发布调用操作以更新 div 的 _show.js.erb 文件,但根据错误消息,该过程似乎没有那么远。

【问题讨论】:

  • 问题似乎出在你的 rouse.rb 中,你能发一下吗?
  • @user 是否为空?您是否向用户资源添加了新的 RESTful 操作?另外,我认为您应该将@user 传递给user_userposts_path,如果您已将其添加为:on =&gt; :member 方法,如routing docs 中所述。

标签: ruby-on-rails ruby ajax


【解决方案1】:

假设你有以下几点:

# /app/models/user.rb
class User < ActiveRecord::Base
  has_many :posts
end

# /app/models/post.rb
class Post < ActiveRecord::Base
  belongs_to :user
end

我会在你的路由文件中添加一个nested resource

#/config/routes.rb
resources :users do
  resources: posts
end

您可以免费获得一堆很棒的“_path”方法(从控制台运行$ rake routes 以查看所有内容),它使您可以访问诸如 /users/123/posts 之类的 URL。此请求将转到 PostsController 的 index 方法,并将自动在 params 哈希中包含 :user_id => 123 。然后,您可以执行以下操作:

# In your view:
<%= link_to "List of all posts", user_posts_path(@user), :remote => true %>
<div id="posts"></div>

# /app/controllers/posts_controller.rb
class PostsController < ApplicationController
  respond_to :js # allows for AJAX requests

  def index
    if params[:user_id].present? # Do this if using the nested resource
      @user = User.find(params[:user_id])
      @posts = @user.posts.order('posts.created_at DESC')
    else # Otherwise, treat it like a normal request
      @posts = Post.all
    end
    respond_with @posts
  end
end

由于您的请求是远程发送的,因此您需要一个相应的“js”版本的索引视图(注意下面的文件名并查看this Railscast for more explanation):

# /app/views/posts/index.js.erb
$('#posts').html("<%= escape_javascript(render(@posts)) %>");

这会将帖子渲染到 &lt;div id="posts"&gt; 标记中。 (您可能还需要 /app/views/posts/ 中的“_post.html.erb”部分。)

但是,说了这么多,您确定需要通过 AJAX 执行此操作吗?您可以简单地在 UsersController#show 方法中预加载所有帖子,最初使用 CSS 隐藏列表,然后在该链接上添加一个 jQuery toggle() 方法。无论如何,希望这是有道理的并且是有帮助的。

【讨论】:

  • 非常感谢,这是我忽略的事情(我很累,将 js 文件命名为“_userposts.js.erb”而不是“userposts.js.erb”,而我最终还是使用了你的发布的方法让这个工作我也恢复到我的旧代码,看看为什么它没有工作,并且文件名更改我的旧代码是正确的。但无论如何,谢谢你的帮助。
猜你喜欢
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
  • 2018-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
相关资源
最近更新 更多