HI
I tried using a javascript function call in the submit tag of a form_remote_for but it is not able to find the function im calling.
if i call the same function from form_remote_for then ajax stops working.
can ny one help me how can i call an javascript function when im using form_remote_for NOT FORM_REMOTE_TAG....????
I think REMOTE_FORM_FOR is what you need.
example:
In your view:
<%- remote_form_for(comment, :url => topic_post_comments_path(#topic, post),
:after => "submitComment(self);$('input').disable()") do |f| %>
<%= f.text_field :body, :size => 70, :class => "comment_body" %><br />
<%= f.submit "Submit", :class => "comment_submit" %>
<%- end -%>
Notice: the javascript function in :after is my custom javascript functions.
And in your controller (it's comments_controller here)
#comment = #post.comments.new params[:comment] # actually, it depends on your model :p
respond_to do |format|
# remember to handle exception here. like if #comment.save or not
format.html
format.js {
render :update do |page|
pagepage.visual_effect :highlight, "comments"
end
}
end
anyway it's just a easy sample, you have to handle more details after you get some feeling for remote_form_for.
Good luck.
Related
I've got a Rails application that is using nested forms. Details follow and I tried this solution (Rails 3 Nested Models unknown attribute Error), but for some reason, the field is repeating multiple times instead of listing and saving the options correctly. Thanks in advance for your help!
Model information for Newsavedmaps
has_many :waypoints, :dependent => :destroy
accepts_nested_attributes_for :waypoints
Newsavedmap_controller
def new
#newsavedmap = Newsavedmap.new
waypoint = #newsavedmap.waypoints.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #newsavedmap }
end
end
def edit
#newsavedmap = Newsavedmap.find(params[:id])
if #newsavedmap.itinerary.user_id == current_user.id
respond_to do |format|
format.html # edit.html.erb
format.xml { render :xml => #activity }
end
else
redirect_to '/'
end
end
Maptry View
<% form_for #newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.error_messages %>
<% f.fields_for :waypoint do |w| %>
<%= w.select :waypointaddress, options_for_select(Waypoint.find(:all, :conditions => {:newsavedmap_id => params[:newsavedmap_id]}).collect {|wp| [wp.waypointaddress, wp.waypointaddress] }), {:include_blank => true}, {:multiple => true, :class => "mobile-waypoints-remove", :id =>"waypoints"} %>
<% end %>
<% end %>
When I use the above code, my form works correctly, but submitting it gives me this error:
UnknownAttributeError (unknown attribute: waypoint)
When I change ":waypoint do |w|" to ":waypoints do |w|" in the view, the select field disappears when the user is creating a new record, and in the edit view, the select field appears several times (however many waypoints the user saved in the record.)
How can I get this form field to work properly?
EDIT 1
Here is my latest attempt. For new records, the select field does not appear. However, in the edit view, the select field appears multiple times. This is a Rails 2 application, FYI. Taking a cue from the comments, I used a collection_select approach (not collection_for_select because I couldn't find documentation for that.) Again, I appreciate your help!
<% f.fields_for :waypoints do |w| %>
<%= w.collection_select( :waypointaddress, #newsavedmap.waypoints, :waypointaddress, :waypointaddress, {:include_blank => true}, {:id =>"waypoints"} ) %>
<% end %>
Your form has the following problems.
Use f.fields_for :waypoints since the argument needs to match the name of the association.
Use collection_select rather than select, since you have an ActiveRecord model behind that field.
So, taking that into account, you could try this for your form:
<% form_for #newsavedmap, :html => { :id => 'createaMap' } do |f| %>
<%= f.error_messages %>
<% f.fields_for :waypoints do |w| %>
<%= w.collection_for_select :waypoint, :waypointaddress, #newsavedmap.waypoints, :waypointaddress, :waypointaddress, { :include_blank => true }, { :multiple => true, :class => "mobile-waypoints-remove", :id =>"waypoints" } %>
<% end %>
<% end %>
The API for collection_select is a bit tricky to get right. I usually need a few attempts as well. This previous SO question may help clear things up: Can someone explain collection_select to me in clear, simple terms?
Tis is my view:
<%= form_for item, :url => comment_item_path(item), :html => {:remote => true, 'portal-transform' => true, :multipart => true} do |f| -%>
<%= f.fields_for :updates, Update.new, :index => nil do |m| -%>
<%= m.text_area :comment %><br />
<%= m.file_field :attachment %>
<% end -%>
<%= f.submit "Comment" %>
<% end -%>
And controller action:
respond_to do |format|
format.js do
render :json => {}
end
end
When I submit the form with only comment (text_area) field entered and keep attachment (file_field) field blank, it render exactly what expected.
But when I submit the form with attachment, it resulted in:
Completed 406 Not Acceptable in 56ms
What went wrong for me? Please guide.
Thanks.
Browsers do not allow file uploads via AJAX for security reasons. If you leave the form's file_field blank however, the form submits normally with no error, which explains the behaviour you are seeing.
To upload files via AJAX in Rails 3, you can use the Remotipart gem.
http://os.alfajango.com/remotipart/
Here is an example usage:
http://thechangelog.com/post/7576700785/remotipart-rails-3-ajax-file-uploads-made-easy
I have an erb file named index that has a form in it. As a simple example:
<% form_for #foo, :url => {:action => 'bar'} do |f| %>
<%= f.submit "BAR!" %>
<%end%>
When I click the BAR! button it preforms the actions I expect and it forwards me onto the bar.erb file, displaying the expected output. What I would like to be able to do, however, is to take the generated html from this page and stuff it into the innerHTML of a div on the index page. I assume there is a way but I must ask, is there a way to achieve this? Are there any examples available that would be helpful? Thanks!
You should be able to pass the id of the div to update like so:
<% remote_form_for #foo, :url => {:action => 'bar'}, :update => 'id-of-div-to-update' do |f| %>
<%= f.submit "BAR!" %>
<%end%>
In the controller:
def bar
# your code here
respond_to do |format|
format.html { redirect_to(xxx) }
format.js
end
end
Rails will look for a template named bar.js and will render it and return it's content to the browser without a redirect.
I am trying to allow for voting on posts in a Ruby on Rails blog that I created. I don't really want to go the plugin or gem route because I think I am close. Here is the situation:
I am displaying all my posts in /posts/index.html.erb through the partial _post.html.erb. With each post I am rendering the following code for the vote button:
<% remote_form_for [#post, Vote.new] do |f| %>
<%= f.hidden_field :ip_address, :value => "#{request.remote_ip}" %>
<%= submit_tag 'Me Too', :class => 'voteup' %>
<% end %>
Which sends the request through the votes controller:
def create
#post = Post.find(params[:post_id])
#vote = #post.votes.create!(params[:vote])
respond_to do |format|
format.html { redirect_to #posts}
format.js
end
end
The error I am getting is Couldn't find Post without an ID which makes sense because I am not on the /posts/show.html.erb page.
Is there a way that I can pass the post_id to the votes_controller (and into the post_id column in the votes table) from the /posts/index.html.erb view?
Try adding this in the view, after the remote_form_for :
<%= f.hidden_field :post_id, :value => #post.id %>
You might also need to to this in the def create
#post = Post.find(params[:vote][:post_id]) #note the added [:vote]
If you look at the params after those changes, it should show:
{"vote"=>{"ip_address"=>"127.0.0.1", "post_id"=> >>actual-post-id-here<< },
"commit"=>"Vote Up ↑" ..........
Also, I recommend having a look at the excellent RailsCasts by Ryan Bates. Lots of great video tutorials there.
So my background is in Java web services, but I'm trying to make the move to ROR.
I'm using FlexImage to handle image uploading and thumbnail generation. I followed the guide and CRUD behavior was working fine at one point. However, at some point, CRUD behavior for one of my models (Images) was broken.
The error code I'm getting back is as follows: ActiveRecord::RecordNotFound in ImagesController#show -- Couldn't find Image with ID=#<Image:0x4e2bd74>. In other words, when I'm telling Rails to create/update/destroy, it is confusing the object with the id. This seems to indicate there might be a routing issue. I thought adding a partial for images might have been the trouble, but rolling back the changes didn't fix it.
Following are the new, show and update methods of the controller for the Images model:
# images_controller.rb
# ...
def new
#image = Image.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #image }
end
end
# ...
def show
#image = Image.find(params[:id])
respond_to do |format|
format.jpg # show.jpg.erb
format.html # show.html.erb
format.xml { render :xml => #image }
end
end
# ...
def create
#image = Image.new(params[:image])
if #image.save
redirect_to image_url(#image)
else
flash[:notice] = 'Your image did not pass validation.'
render :action => 'new'
end
end
# ...
Note that show() is, of course, expecting an appropriate id. Here's the new.html.erb for uploading a new image:
# new.html.erb [upload image]
<h1>New image</h1>
<% form_for #image, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<table><tr><td width="50%">
<p>
<%= f.label :filename %><br />
<%= f.text_field :filename %></p>
</td>
<td><p><b>Upload Image</b><br />
<%= f.file_field :image_file %><br />
or URL: <%= f.text_field :image_file_url %>
<%= f.hidden_field :image_file_temp %>
</td>
<td>
<b>Uploaded Image:</b><br />
<%= embedded_image_tag(#image.operate { |img| img.resize 100 }) if #image.has_image? %>
</td>
</tr>
</table>
<p>
<%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', images_path %>
The relevant portion of routes.rb are as follows:
# routes.rb [excerpt]
map.resources :images
map.image 'images/:action/:id.:format', :controller => 'images'
Also note that a new image does actually get uploaded and the error is thrown on redirect to show (which is expecting a valid ID in params[:id] and not the object which for whatever reason it is being handed.)
Thanks for your help in advance, and please let me know if anything jumps out at you.
From looking at the code it appears to me that the problem may be caused by using image_url(#image) in combination with the non-RESTful image route.
You will probably want to remove the line
map.image 'images/:action/:id.:format', :controller => 'images'
from your routes.rb.
The line
map.resources :images
should actually be enough to expose all CRUD actions in your ImagesController.
My suggestion is to use ruby-debug and set a break point right before the Image.find call. Inspect params[:id] and see what it actually is.
A more ghetto approach, put this before the Image.find call
logger.info params[:id].class
and see what is in that variable. Is it possible that you have some sort of before filter that is manipulating it?
try
redirect_to :action => "show", :id => #image
I think that's a more idiomatic way to code the redirect. And +1 to molf's advice about RESTful routes.