My view contains this link to redirect to the index page for people.
<%= link_to 'No', '/people' %>
I want to send an extra flag to the index method of the people controller so it will do something extra in case this link is clicked. I've tried a ton of different things, none of which work. I tried using the more complicated syntax with :controller => :people_controller, :action => :index...but since i'm coming from the show view it sends the ID and messes up my routes.
How can i send an extra parameter with this link_to?
<%= link_to 'No', people_path(extra_parameter: "Veg") %>
Related
I have two rails helper on my application helper:
def active_class(link_path)
current_page?(link_path) ? 'active' : ''
end
def active_class_white(link_path)
current_page?(link_path) ? 'active-white' : ''
end
One is for regular links the other one is for the submenus. Usually I place the link like this:
<%= link_to "Home", root_path(:anchor => 'home'), class: "nav-link #{active_class('/')}", :"data-id" => "home" %>
Now here's my problem. On my homepage I got this link where it will slide to a particular section of the site thus requires a character like #about. If I place:
<%= link_to "About", root_path(:anchor => 'about'), class: "nav-link #{active_class('/#about')}", :"data-id" => "about" %>
It's still putting the active class on the home text instead of the about (the homepage is a one page slider type).
Another thing is that for complicated url like the devise edit profile, I tried to put the ff:
<%= link_to "Edit Profile", edit_user_registration_path(current_user), class: "dropdown-item #{active_class_white('/users/edit/:id')}" %>
Placing /users/edit/:id doesn't work on this kind of URL: http://localhost:3000/users/edit.13
On this two kinds of URL my code doesn't work. Any idea how to make them work or turn this around?
Anchors are purely client-side and are not sent along with the request to the server. So, the only way to implement the first part of your question is through Javascript. You can listen for a click event on links then add the active class to the one that was clicked.
The second part of your question, where you pass in the :id segment key, can be solved by passing a route helper (along with an object) to current_page? instead of an explicit string...
class: <%= active_class(edit_user_registration_path(current_user)) %>
Is it possible to change the contents of view when click the different link?
I have two links.
<%= link_to "Add event", xxx_path %>
<%= link_to "Add place", xxx_path %>
I'd like to display <%= f.text_field :detail %> on the view of xxx_path when I click Add event, and not to display it when I click Add place.
It would be appreciated if you could give me advice.
If you need to pass additional information along with a GET request you would use query parameters.
example:
/cities?near=london
/users/1/orders?status=pending
You can pass any hash keys with path/url helpers to generate a path/url with any arbitrary query attached.
cities_path(near: 'london')
user_orders_path(user_id: current_user.to_param, status: 'pending')
However your example makes very little sense as Event and Place most likely are different resources and should have their own routes and controllers.
I met a problem of parameter passing in Rails.
I have an object named 'account' and another object named 'locale'. There is a one-to-many relationship between them. One locale can be mapped with multiple accounts.
On accounts/index.html.erb, I have a dropdown which will list all the available locales.
And I have a link below.
In expectation, when I clicks this link, the index method of account controller will be called and the value of selected locale id will be passed. And the index method will retrieve all the accounts belonging to that locale.
What blocks me is I have no idea how to pass that selected value of dropdown to the controller.
I only know the basic way of passing a parameter is:
<%= link_to 'Refresh', {:action => 'index', :fromvar => 'refresh', :selected_locale_id => '1'}.
And from controller, we can get it by:
params[:selected_locale_id]
But it is the case of a fixed value. How to deal with a dynamic UI control value as my case?
Does link_to support some javascript to be embedded?
My rails version is 3.2.13.
Any one has idea on this?
You can either do this as a form, or as javascript in the onclick event of the link. I would recommend a form.
The following should get you started:
<%= form_for :index do |form| %>
<%= form.select :locale_id, Locale.all %>
<%= form.submit %>
<% end %>
Be sure to add a post route to the index action to allow the form to submit.
An apartment_listing has many reviews, and a review belongs to an apartment_listing.
In the file views/apartment_listings/show.html.erb, I show a list of reviews for that particular apartment_listing. These reviews are generated with the partial view apartment_listings/_review.html.erb like so:
<%= render :partial => "review", :collection => #apartment_listing.reviews %>
In _review, I want to have a button that, when pressed:
Increments that review's helpful_count attribute.
Makes it so that it cannot be pressed again while in the same browser - probably using cookies.
I feel like the former shouldn't be too hard to figure out, but it's got me beat. I'm really not sure where to start with the second goal.
EDIT: I managed to update the review's helpful_count attribute with this code in apartment_listings/_review.html.erb:
<%= form_for review, :method => :put, :remote => true do |f| %>
<%= f.hidden_field :helpful_count, value: (review.helpful_count + 1) % >
<%= f.submit 'Helpful?' %>
<% end %>
However, I'm not sure if this is the best way to do it, and I'd like to be able to disable the button after it is clicked.
Your code for updating helpful_count has the potential for problems. Imagine two users have loaded an apartment on their web page. One of them marks it helpful, and the next one does as well. Since when they initially loaded the page, helpful_count was the same, after both of them click helpful, the count will only be incremented by one: it would be updated twice to the same value.
Really, you want to create a new action, probably under the reviews resource for an apartment. That action could use ActiveRecord's increment method to update the helpful_count (technically there's still a race condition in increment!, you'd encounter it much less often) http://apidock.com/rails/ActiveRecord/Persistence/increment%21
Cookies seem like a reasonable solution for the latter problem. Simply bind to submit on the form with jQuery, and create the cookie in the handler.
What does the code look like in your reviews controller? More experienced RESTful coders might be able to speak more coherently on this, but the way I see it, incrementing the helpful_count attribute should be an action sent to the reviews controller. That way, you can create a link that performs the action asynchronously.
For example, inside _review.html.erb:
<% collection.each do |review| %>
<%= link_to "Mark as Helpful", "/apartment_listing/#{#apartment_listing.id}/reviews/#{#review.id}/incHelpful?nonce=#{SecureRandom.rand(16)}", :remote => true, :method => :put %>
# ... Do something cool with your review content ...
<% end %>
Inside your ReviewsController class:
def incHelpful
unless params[:nonce] == session[:nonce][params[:id]]
#review = Review.find(params[:id])
#review.helpful_count += 1
#review.update_attributes(:helpful_count)
session[:nonce][params[:id]] = params[:nonce]
end
render :nothing
# Optionally return some javascript or JSON back to the browser on success/error
end
Inside /config/routes.rb:
put "apartment_listing/:apart_id/reviews/:id/incHelpful" => "reviews#incHelpful"
The main idea here is that actions that edit a resource should use the PUT http method, and that change should be handled by that resource's controller. Rails' built-in AJAX functions are engaged by setting :remote => true inside the link_to helper. The second concept is that of a nonce, a random value that is only valid once. Once this value is set in the user's session, subsequent requests to incHelpful will do nothing.
I have a slightly complex navigational system with numerous landing pages, multipage forms, and multiple ways of accessing the standard CRUD functions.
Objective: To maintain a variable such as (params[:target]) throughout the system such that each controller knows where to redirect a user based on the location and circumstances of the link_to.
How to implement this the best way?
Is there a better way to store navigation markers so any controller and method can access them for the current_user?
If using params[:target] is a good way to go (combined with if or case statements in the controller for the redirect), how do I add the target params to the form when adding or editing a record? For example, view says:
# customers/account.html.erb
<%= link_to "edit", :controller => "customers", :action => "edit", :id => #customer.id, :target => "account" %>
# customers/edit.html.erb
<%= submit_tag "Update", :class => "submit" %>
# how to send params[:target] along with this submit_tag so the update method knows where to redirect_to?
Thank you very much.
I think you could get the same result by setting a session[:target] each time is necessary. so you'll always know where to redirect from controllers without changing link_to params and leaving clean URLs.
hope this helps,
a.