remote form_tag in rails3 without a named route
The only way I found to do it is to wrap the url parameters in the url_for method.
form_tag url_for(:action => :create, :id => @artist.id), :remote => true do
However, if you need to the pass the method
parameter you might need to wrap that and the remote in parentheses.
Rails form_tag routing to non-Restful action without including name of action in url on the browser
@Margo thx again for I've taken your inputs. I figured out why form went to destroy instead of going to update. Actually there was a button_to for deleting attachment and as soon as I changed to link_to the form went to the right controller method.And I never suspected an innocuous looking button which was not 'form Submit'-button, was the culprit!
Here's the solution: routes.rb
resources :cv_attachments, only: [:index, :show, :create, :new, :destroy] do
collection do
put 'update_multiple'
end
end
index.html--only giving part of form
<tbody>
<% @cv_attachments.each do |cv_attachment| %>
<% if cv_attachment.main %>
<%= hidden_field_tag "ex_main", cv_attachment.id %>
<% end %>
<tr>
<td><%= radio_button_tag "new_main", cv_attachment.id, cv_attachment.main, :id => "#{cv_attachment.id}"%> </td>
<td><%= cv_attachment.attachment.file.basename %></td>
<td><%= cv_attachment.posting_date %></td>
<td><%= link_to "Delete", user_cv_attachment_path(current_user, cv_attachment), method: "delete", data: { confirm: 'Are you sure?' }, class: "btn btn-danger btn-outline btn-md" %></td>
</tr>
<% end %>
</tbody>
Controller:
def update_multiple
if request.put?
if params["ex_main"] != params["new_main"]
CvAttachment.find(params[:ex_main]).toggle!(:main)
CvAttachment.find(params[:new_main]).toggle!(:main)
end
end
Rails 3.0 form_tag Routing
You can add resources route for sessions:
resoures :sessions
and fix form:
= form_tag sessions_path do
...
Remember rename you login action in sessions controller to new
UPD:
or you can add named route for 'session/login'
match 'session/login' => 'session#login', :as => :new_session
and use:
= form_tag new_session_path do
rails3 form_tag
Your partial must be rendered inside the form_tag
to be a part of the form.
remote form_tag rails3
Look at your params:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"1zJCwY0sXb4TaTpO2d+MLox2CHk1sBpho/JR4oH18sw=", "bgUploaderFieldName"=>"http://upload.contextoptional.com/chag/assets/20110608172149.csv", "promotion_id"=>"{:value=>2}", "commit"=>"Add multiple", "method"=>"post"}
You don't have a key named :csv_upload but that's what you're using to set the attributes in your #create action
You should be using form_for:
<%= form_for @csv_upload, :remote => true do |f| %>
<%= f.text_field :bgUploaderFieldName %>
...
<% end %>
or... at the very least, you should give the tags their proper name:
= form_tag csv_uploads_path(:method=> :post), :remote => true, :disable_with => 'Adding multiple...' do
= text_field :csv_upload, "bgUploaderFieldName", "", :id => "bgUploaderField", :readonly => "true"
= hidden_field :csv_upload, :promotion_id, :value => @promotion.id unless @promotion.nil?
= submit_tag 'Add multiple'
Rails form_tag redirect route error
I think this is a case of using GET for the route and POST upon form submission.
If you run rake routes
you should see the route is a GET, right? When a form submits it makes a POST request. Either make the form_tag like this:
form_tag({:controller => "admins", :action => "check_in"}, :method => "get")
or change the route to POST in the routes.rb file like so:
post "admins/check_in" => "admins#check_in"
You can see, when you start rails with rails s
in the terminal, what kind of request it receives by reading the request log as it comes in.
Hope that helps, otherwise:
- http://guides.rubyonrails.org/routing.html
- http://guides.rubyonrails.org/form_helpers.html
Related Topics
Ruby -- Capitalize First Letter of Every Sentence in a Paragraph
More Concise Version of Max/Min Without the Block
Ruby Ternary Operator Structure
Scraping a Site That Requires Login Username and Password on Two Separate Pages
How to Stop Savon from Adding Prefixes to Soap.Body
Ruby on Rails: Params Is Nil. Undefined Method '[]' for Nil:Nilclass
How to Include Ё in [А-Я] Regexp Char Interval
Building a Simple Search Form in Rails
Can't Setup Ruby Environment - Installing Fii Gem Error
Why Is 032 Different Than 32 in Ruby
Rails Query Timestamp Between Two Hours
Ruby Looks for Class Variable in the Object Instead of Specific Class
Gem Install Rails Doesn't Work Due to Openssl/Etimedout in Windows
Using Delta Indexes for Associations in Thinking Sphinx
Can a Watir Browser Object Be Re-Used in a Later Ruby Process