Rails - Pass Id Parameter on a Link_To

rails - Pass id parameter on a link_to

If your bids are not nested resource of the project, then you can add project_id as parameter to the path:

<%= link_to "New Bid", new_bid_path(:project => @project.id) %>

def new  
@bid = Bid.new
@project = Project.find(params[:project])
end

otherwise:

#routes.rb

map.resources :projects do |project|
project.resources :bids
end

<%= link_to "New Bid", new_project_bid_path(@project) %>

def new  
@project = Project.find(params[:project_id])
@bid = @project.bids.build
end

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.

passing id parameter from show action to new action via link_to, rails

You should be able to just set the values in your new action:

def new
@product = Product.new
@product.category_id = params[:id]
@product.name = params[:name]
end

Then they will appear as such in your form.

But I don't think it's a good idea to call your parameter id because it's a default parameter name for resourceful routing. params[:id] in the ProductsController should typically always refer to a Product object. As it happens, you're using it in the new action which in normal use will never receive an id parameter so I doubt you'll get in any trouble, but it isn't very semantic.

If you let Product accept nested attributes for Category then I think you'd be able to simply do:

@product = Product.new(params[:product])

if you structured your params like: :product => { :name => "whatever", :category_attributes => { :id => 1 } } but that might be overkill depending on your needs.

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

rails - Pass id parameter on a link_to / routing

You want get 'orders/checkout', corresponding to the plural form of "order" used in your controller name.

passing id to controller through link_to in railsner


<%= link_to "category.name", products_path(:product_id => product.id) %>

Then in your method you can access the value through:

params[:product_id]

pass parameter by link_to ruby on rails

Try:

<%= link_to "Add to cart", {:controller => "car", :action => "add_to_cart", :car => car.id }%>

and then in your controller

@car = Car.find(params[:car])

which, will find in your 'cars' table (as with rails pluralization) in your DB a car with id == to car.id

hope it helps! happy coding

more than a year later, but if you see it or anyone does, i could use the points ;D

Rails Pass additional parameters in link_to

Your approach is correct, but the host option in your route helper (and all other derivatives of url_for) is reserved for modifying the host in the generated URLs. But since you're using issue_path and not issue_url, the host parameter is just being ignored. To fix your issue, pick another parameter name.

How to add params id in rails link_to

Rails routes can take arguments; if you ever want to explicitly pass a parameter to a route you can do so just like you would pass an argument to any other method:

<%= link_to "Create Job", new_company_job_path(company_id: @company.id) %>

*note: this assumes you have defined @company somewhere on this view.

In the case of general resource routes, Rails is smart enough to insert these params in the right place. It's worth noting though that if a param is not defined on the route in routes.rb Rails will tack on these passed parameters to the end of the route as query strings.

For example, if you have a route like

get 'landing_pages/page' => '#landing_pages#page'

and you called:

<%= link_to "Go to your landing page", landing_pages_page_path(brand: 'Apple') %>

The route will become /landing_page/page?brand=Apple

For further reference: http://guides.rubyonrails.org/routing.html



Related Topics



Leave a reply



Submit