English 中文(简体)
RoR, params not being adopted to the controller for a :remotelink_to
原标题:RoR, params not being passed to the controller for a :remote link_to

Ok不诚实,即没有花很多时间来寻找解决办法,但我的儿子如何保持我的注意力。 我只想问一个问题,因为问题似乎很简单,但迄今却 st。

因此,为了保持简单的说法,我有用户(w/model)和家庭控制者,家庭是根本的路线。

在根基目录中,我希望能够看到用户使用阿加克斯在主页上用一个部分更新员额清单的所有员额。

用户 控制员 我有一个叫的用户牌

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

And in my view I have

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

我的错误如下:

Started GET "/users/2/userposts" for 127.0.0.1 at Sun Jan 15 13:36:23 -0600 2012 Processing by UsersController#userposts as JS Parameters: {"user_id"=>"2"} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1 Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method posts for nil:NilClass):
app/controllers/users_controller.rb:27:in
userposts

Rendered /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) Rendered /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) Rendered /home/n0de/.rvm/gems/ree-1.8.7-2011.03/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (3.2ms)

我确实认识到,我没有张贴<代码>_show.js.erb。 档案要求采取行动更新零件,但根据错误信息,这一过程似乎如此。

最佳回答

假设:

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

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

在您的路线档案中添加“

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

你们从我们的圣殿那里获得一大批免费的“夜总”方法(有<条码> $条/条码>,以便看看所有情况),并且使你们能够接触到用户/用户/123/职位等URLs。 这项请求将采用你的邮政主计长的指数方法,并将自动包括:用户_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”版本的您的指数观点(脚注如下,见,该铁路预测需要更多的解释):

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

这将使这些员额列入<代码><div id=“posts”>。 (青年很可能需要部分在/申请/调查/职位/职位中“_post.html.erb”。)

然而,在说了这一切之后,你是否确信你需要通过非洲复兴开发银行这样做? 您可以简单地把用户“冰箱”方法中的所有职位上上载,先用CSS来掩盖名单,然后在这种联系上添加“ j”方法。 无论如何,这种希望是有意义和有益的。

问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签