Rails - passing parameters in link_to
First of all, link_to is a html tag helper, its second argument is the url, followed by html_options. What you would like is to pass account_id as a url parameter to the path.
If you have set up named routes correctly in routes.rb, you can use path helpers.
link_to "+ Service", new_my_service_path(:account_id => acct.id)
I think the best practice is to pass model values as a param nested within :
link_to "+ Service", new_my_service_path(:my_service => { :account_id => acct.id })
# my_services_controller.rb
def new
@my_service = MyService.new(params[:my_service])
end
And you need to control that account_id is allowed for 'mass assignment'. In rails 3 you can use powerful controls to filter valid params within the controller where it belongs. I highly recommend.
http://apidock.com/rails/ActiveModel/MassAssignmentSecurity/ClassMethods
Also note that if account_id is not freely set by the user (e.g., a user can only submit a service for the own single account_id, then it is better practice not to send it via the request, but set it within the controller by adding something like:
@my_service.account_id = current_user.account_id
You can surely combine the two if you only allow users to create service on their own account, but allow admin to create anyone's by using roles in attr_accessible.
hope this helps
How to pass parameters with link_to tag?
You're already doing it correctly in the controller, you just need to include the :id in the link_to url options:
<%= link_to(:controller => "posts", :action => "up vote", :id => @post) do %>
<span>Like</span>
<span>(<%= @post.get_upvotes.size %>)</span>
<% end %>
Note: you don't need to call id
on the post, rails will automatically figure this out for you.
A better solution, would be to use resource routes for you Post object. then add upvote/downvote actions:
# in config/routes.rb
resources :posts do
member do
match "upvote", :as => :upvote, :via => [:get, :put]
end
end
Rails will automatically create url helpers that you can use in your views
# in show html.erb
<%= link_to(upvote_post_url(@post)) do %>
<span>Like</span>
<span>(<%= @post.get_upvotes.size %>)</span>
<% end %>
the advantage to this is simpler code, and the ability change your routes with having to update template code.
Rails has already done all the heavy lifting of how to make and link to urls, to be faster at plumbing rails views and controllers together, you should definitely read the official guides http://guides.rubyonrails.org/routing.html, then may look daunting at first, but they will quickly become second knowledge.
link_to send parameters along with the url and grab them on target page
Just add them to link:
<%= link_to "Add Product", '/pages/product?param1=value1¶m2=value2' %>
and in controller:
param1 = params[:param1] # "value1"
param2 = params[:param2] # "value2"
If you use helper methods for routes (for example company_path
), then you can add hash of params, so this two should be similar:
<%= link_to "Add Product", new_product_path(:param1 => "value1", :param2 => "value2") %>
<%= link_to "Add Product", "/products/new?param1=value1¶m2=value2" %>
From documentation:
link_to "Comment wall", profile_path(@profile, :anchor => "wall")
# => <a href="/profiles/1#wall">Comment wall</a>
link_to "Ruby on Rails search", :controller => "searches", :query => "ruby on rails"
# => <a href="/searches?query=ruby+on+rails">Ruby on Rails search</a>
link_to "Nonsense search", searches_path(:foo => "bar", :baz => "quux")
# => <a href="/searches?foo=bar&baz=quux">Nonsense search</a>
Ruby on Rails - passing parameters in link_to with if conditions?
Conditionally pass a hash, containing additional params for this_link_path
url helper.
<%= link_to "This is a link", this_link_path( ({company: params[:company] } if params[:company]) ), class: tl(this_link_path) %>
To be more concise you can compact the hash.
<%= link_to "This is a link", this_link_path({company: params[:company]}.compact), class: tl(this_link_path) %>
If you know that you'll need it more often, wrap this_link_path
call in a custom helper. Hash can contain additional params, including ones with fixed values.
def this_link_with_additional_params_path
this_link_path({company: params[:company], name: 'test'}.compact)
end
Then in view you can use:
<%= link_to "This is a link", this_link_with_additional_params_path, class: tl(this_link_path) %>
Pass additional parameters with link_to
I resolved my problem. Make sure the value you give to your additional parameters is something. When I go to the news index page, even though the selected page is the first one, there is no parameter page=1
. The parameter only appears once click on a different page (or you go to another page and come back to the first one). If you give an additional parameter to the link_to and its value is nothing, the rendered link will be (in my case):
<a href="/news/2" />
Instead of:
<a href="/news/2?back_page=" />
So you might get trapped, just like I was.
Related Topics
Problems with the Rails Console, Rvm and Readline
How to Compare Strings Ignoring the Case
Ruby: File Encryption/Decryption with Private/Public Keys
Openssl::Ssl::Sslerror: Ssl_Connect Returned=1 Errno=0 State=Unknown State: Unknown Protocol
Ruby on Rails Rake Assets:Precompile Error
Does Ruby Provide a Way to Do File.Read() with Specified Encoding
How to Build a Rubygems Mirror Server
Warning: Can't Verify Csrf Token Authenticity in Case of API Development
Rails on Windows Is So Slow (Rails -V Takes 4 Seconds)
How Does Shovel (<<) Operator Work in Ruby Hashes
How to Print Out the Contents of an Object in Rails for Easy Debugging
How to Validate Exits and Aborts in Rspec
How to Ensure That Ruby Uses an Openssl Not Vulnerable to Heartbleed
Prevent Rails Test from Deleting Seed Data
Mocha Mock Carries to Another Test
Activerecord::Adapternotspecified Database Configuration Does Not Specify Adapter
Why Can't a Variable Name End with '' While a Method Name Can