How do I pass objects through a _url based on a routing in rails?
I want to pass the attributes associated with two objects into a path created from a route. In this case, the _url is skip_contact_letter_url. contact_letter and letter are passed thro开发者_开发百科ugh a render partial. The clip below resides in the partial.
<%= link_to_remote "Skip Letter Remote #{contact_letter}",
:url => skip_contact_letter_url(contact_letter, letter),
:update => "update-area-#{contact_letter.id}-#{letter.id}" %>
<span id='update-area-<%="#{contact_letter.id}-#{letter.id}"%>'> </span>
The route I created looks like this:
map.resources :contact_letters, :member => {:skip => :post}
And the controller looks like this:
def skip
@contact_letter = ContactLetter.new(params[:all])
@contact_letter.status = "skipped"
@contact_letter.date_sent = Date.today
#@contact_letter.date_created = Date.today
if @contact_letter.save
render :text => 'This letter was skipped!'
end end
When I look at the console, none of the parameters from contact_letter or letter get passed through.
As a result of the routes, this is what it looks like from rake routes:
skip_contact_email POST /contact_emails/:id/skip(.:format) {:action=>"skip", :controller=>"contact_emails"}
Added Notes:
I am thinking the route needs to be changed so I can pass :contact_id and :letter_id, but not clear:
map.resources 'contacts/:contact_id/letters/:letter_id/skip', :controller => 'contact_letters', :action => 'skip'
First of all, probably you dont have params[:all]. In your routes you can define something like this:
<%= link_to_remote "Skip Letter Remote #{contact_letter}",
:url => skip_contact_letter_url(
:contact_letter=>contact_letter.attributes,
:letter=>letter.attributes)
Note that contact_letter.attributes
produces a hash with the object attributes.
In your controller you will have like:
@contact_letter = ContactLetter.new(params[:contact_letter])
精彩评论