开发者

link_to with remote => true not functioning as expected

I'm using rails 3 and this is how I've set up my link_to in my view

<%= link_to ('add',
                :url => {:controller => 'favourite_companies', :action =>'create', 
                :company_id=>@company.id,   
                :company_name=>@company.company_name, :remote => true}) %>

When I click it, the page refreshes and nothing happens.

I've added <%= csrf_m开发者_开发知识库eta_tag %> and all javascript files.

In the controller, the function looks like this:

def create
  @favorite_list = 
      FavouriteCompany.new(:user_id=>curr_user.id,:company_id=>params[:company_id])
  @favorite_list.save
  render :partial => "create"

end

Any idea what might be wrong? Thanks.


Have you looked at the HTML that is being output via your browsers dev tools (ie firebug, etc)?

I believe you have the :remote => true in the wrong hash.

Try:

<%= link_to ('add',
     {:url => {:controller => 'favourite_companies', :action =>'create', 
     :company_id=>@company.id,   
     :company_name=>@company.company_name}}, :remote => true) %>

Is there a reason you aren't using Rails Routes and generating the link via the standard Rails process like:

<%= link_to 'add', favourite_companies_path(@company), :remote => true %>


Funny I've never used link_to with the remote option.

Whatever, it should work like all ajax calls.

So I think you should:

  • remove from your controller render :partial => "create"

  • create a view create.js.erb

  • put your code in it such as the following:

$('#your_container').html("<%= escape_javascript(render(:partial => 'create')) %>");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜