I've been searching all over and can't figure out why this isn't working.
I'm trying to test a very basic ajax action. Here are my code:
Controller:
def commit
respond_to do |format|
format.html { redirect_to :action => "index" } # see note 1
format.js { render :layout => false } # see note 2
format.js { render :nothing => true }
end
end
View:
<%= link_to "commit", :action => "commit", :remote => true %>
<%= form_tag( :action => "commit", :remote => true, :method => :post) do %>
<%= submit_tag "commit" %>
<% end %>
<div id='message'></div>
commit.js.erb
console.log('committed');
$('#message').html("committed");
The problem is I'd get to the commit method, but the page would reload, which defeats the point of remote=>true
Also the commit.js never seemed to get called.
Note 1: If I exclude this line, I get the blank page to /commit. Including it makes the page just reload
Note 2: I've tried both of these approaches suggested by other SO posts
Note 3: I've tried both using link_to and form_tag
Can anyone help? Thanks!
Why did you put there 2 lines?
format.js { render :layout => false } # see note 2
format.js { render :nothing => true }
Remove the second one!
Replace:
<%= link_to "commit", :action => "commit", :remote => true %>
with:
<%= link_to "commit", commit_path, :remote => true %>
The same with the form:
Make your:
<%= form_tag( :action => "commit", :remote => true, :method => :post) do %>
as:
<%= form_tag(commit_path, :remote => true) do %>
Note: POST is the default behavior, you can omit it from form_tag.
Related
So I was wondering how to work with the link_to method and ajax in Rails 3, when redering different partials.
Example:
Let say I have two link in show.html.erb and every link have a partial to render.
<li><%= link_to "Group1", user_path(#user), { :action => 'group1' , :method => :get, :remote => true} %></li>
<li><%= link_to "Group2", user_path(#user), { :action => 'group2' , :method => :get, :remote => true} %></li>
And we are going to render the partials in this div:
<div id="profile-data">
...render here...
</div>
In the UsersController we have our call methods for each partial:
def group1
respond_to do |format|
format.js
end
end
def group2
respond_to do |format|
format.js
end
end
And of course we have our js files in the view user folder:
group1.js.erb
$("#profile-data").html("<%= escape_javascript(render(:partial => 'group1')) %>");
group2.js.erb
$("#profile-data").html("<%= escape_javascript(render(:partial => 'group2')) %>");
So my question is:
is this the right way to render different partials with ajax? Are I missing something? Do have to route them some way?
This code dosent work right now and I dont know why, any help would be appreciated.
You need to explicitly state that you want to make a javascript request in your link_to.
This can be done be setting the format in the options hash to js with: :format => :js.
So in your case it should look like this:
<li><%= link_to "Group1", user_path(#user), { :action => 'group1' , :method => :get, :remote => true, :format => :js} %></li>
<li><%= link_to "Group2", user_path(#user), { :action => 'group2' , :method => :get, :remote => true, :format => :js} %></li>
The link_to should be somewhat like
<%= link_to "Group1", {group1_users_path, :format => :js} , :method => :get, :remote => true %>
or
<%= link_to "Group1", {:controller=>:users,:action=>:group1, :format => :js} , :method => :get, :remote => true %>
or if it is a member route and needs user_id
<%= link_to "Group1", {group1_users_path(#user) :format => :js} , :method => :get, :remote => true %>
The second param for link_to is the url options so only the url related option go in it, others should be out of the hash or else they will be just passed as params.
Check out more details at rails cocs they have some neat docs and examples
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
You need to have group1 and group2 in routes file
it should be like
resourses :users do
collection do
get "group1"
get "group2"
end
end
this will add helpers group1_user_path and group2_user_path
I recemmond you to go through rails docs thoroughly
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
This symptom has been reported in various contexts, most of which I've already searched yet found no clear answer. Here is my context.
In full view index.html.erb:
<div id="add_thingy" style="display:none;">
<%= form_tag (
:action => 'create',
:remote => true,
:update => "thingy_list",
:position => :bottom,
:html => {:id => 'thingy_form'}
) do %>
ipv4_address: <%= text_field "thingy", "ipv4_address" %>
<%= submit_tag 'Add' %>
<% end %>
</div>
In partial view _index.html.erb:
<li><b>
<%= link_to thingy.ipv4_address, "http://"+thingy.ipv4_address+"/index.html", :target => '_blank' %>
<%= link_to 'page1', "http://"+thingy.ipv4_address+"/page1.html", :target => '_blank' %>
<%= link_to 'page2', "http://"+thingy.ipv4_address+"/index.html", :target => '_blank' %>
<%= link_to 'Edit', {:action => "edit", :id => thingy.id} %>
<%= link_to 'Delete', {:action => "delete", :id => thingy.id},
:confirm => "Delete thingy at #thingy.ipv4_address?", :method => :delete %>
</b></li>
In thingy_controller.rb (other things I've tried are shown commented out):
class ThingyController < ApplicationController
layout 'standard'
def index
...
end
...
def create
#thingy = Thingy.new(params[:thingy])
if #thingy.save
# redirect_to :action => 'index'
# $(“#index”).rhtml(“<%= escape_javascript render( #thingy ) %>”);
# render :partial => 'index', :object => #thingy_list
# render :partial => 'index'
render :partial => 'index', :object => #thingy
end
end
...
end
All other aspects of my implementation test clean, including the first commented line above. It's when I try any of the subsequent 4 lines that I get the indicated errors.
I'll use the first commented line for now, though it defeats the main purpose of using Ajax.
Speculation abounds on this question, so I'm looking for information on how it has been fixed in a similar context rather than suggestions on how it might be fixed.
On behalf of myself and the community, thanks for your help!
My setup: Rails 3.0.9, Ruby 1.9.2
I added a custom action to a nested resource task.
routes.rb
resources :tasks
resources :projects do
resources :tasks, :constraints => { :protocol => "http" } do
put :cancel, :on => :member
end
end
rake routes shows
cancel_project_task PUT /projects/:task_id/tasks/:id/cancel(.:format) {:protocol=>"http", :action=>"cancel", :controller=>"tasks"}
In my controller,
tasks_controller.rb
def cancel
#task = Task.find(params[:id])
respond_to do |format|
if #task.cancel
format.html { redirect_to(#task, :notice => 'Task was successfully canceled.') }
else
format.html { render :action => "edit" }
end
end
end
I need to define a form to perform the action, here's what I have currently
_form.html.erb for subscription
<%= form_for [#project, #task], :url => { :action => 'cancel' } do |f| %>
<%= f.submit "Cancel your task"%>
<% end %>
It throws an error
No route matches {:action=>"cancel", :controller=>"tasks"}
I also tried adding :method => "put" with the same error
_form.html.erb for subscription
<%= form_for [#project, #task], :url => { :action => 'cancel', :method => "put" } do |f| %>
<%= f.submit "Cancel your task"%>
<% end %>
Anyone knows the correct form_format syntax to accomplish this?
In case anyone wants the answer, here's what I had to do
<%= form_for [#project, #task], :url => cancel_project_task_path(#project, #task) do |f| %>
It took me way too long to figure this out, hopefully this helps the next unsuspecting developer.
i am using rails 2.3.11. I have a text_field_with_autocomplete thing used inside a form of model blog
<% remote_form_for :blog, :url => {:controller => "blogs", :action =>"add_blog"} do |f|%>
<%= f.text_field :title, :class => "admtxt1 wd1 lfloat" %>
<%= text_field_with_auto_complete "user_login", :login, { :size => 15}, { :url => {:controller => 'users', :action => 'autocomplete_for_blogowners'}, :method => :get, :param_name => 'term', :indicator => "spinner"} %>
<%= submit_tag "Save", :class => "btn-small margintop10" %>
<%end%>
In the blogs_controller,add_blog action i have
def add_blog
unless params[:blog].nil?
#blog = Blog.new(params[:blog])
#user = User.find_by_login(params["user_login"][:login])
#blog.owner = #user
end
respond_to do |format|
if #blog.save
#saved = true
format.js { render :action => 'add_blog' }
else
#saved = nil
format.js { render :action => 'add_blog' }
end
end
end
In my add_blog.js.rjs file
if #saved.nil?
page.replace_html 'message','<div id="msg" class = "textcolor3">' + #blog.errors.full_messages.join('<br />') + '</div>'
else
page.replace_html 'message','<div id="msg" class = "h3txt textcolor3"> Blog created successfully </div>'
end
My blog tablehas id,title,owner_id
And my model has the validation
validates_presence_of :owner_id, :title
The above code works perfectly with the validation of owner shouldnt be blank
But it didnt checks for the wrong user
For eg. if i am typing something where such a user doesn't exists .In that case if i am submitting the form with no results found in the box , Even tat time i am getting Owner shouldn't be blank.
How to check the existence of the User in this case and to add that in the error messages.
Your code is very strange. Why don't you use Rails 3?
Why don't you use REST and CRUD ?
Sorry but it is better to rewrite code absolutely.
It seems as php code, have you used php?
I have the following code:
<%= link_to "PDF", :action => "showpdf", :id => "#{#letter.id}.pdf" %>
'showpdf' is an action in my Letters controller.
My expectation is that this link should yield the following:
http://domain.com/letters/showpdf/id.pdf
But instead, I get:
http://domain.com/letters/showpdf?id.pdf
If the default routes are :controller/:action/:id shouldn't this work?
Do I need to do something in the routes, even though the format for default appear right? Thanks!
Have you tried something like:
<%= link_to "PDF", :action => "show", :id => letter.id, :format => :pdf %>
where your route would be
:controller/:action/:id.:format
and in your controllers "show" action:
respond_to do |format|
format.pdf .....
format.html .....
end