I have a page with the following code:
<div>
<% if #site.roster_management_enabled? %>
<h3><%= link_to "Go To Page", { :controller => 'roster_player_import', :action => 'rostering_redirect'}, :target => "_blank" %></h3>
<% end %>
</div>
The controller/action mentioned above is as follows:
def rostering_redirect
url = URI.join(#site.boss_organization.admin_app_root_url, 'rostering')
redirect_to url.to_s
end
I want the html code listed above to execute automatically rather than having the user click on the link. To be more specific, is there a way to return the URL from the controller/action and pass that to the HTML code such that I don't have to use the syntax {:controller =>.....} and instead use something like URL=....
You can put the url you redirect to directly in your template:
<div>
<% if #site.roster_management_enabled? %>
<h3><%= link_to 'Go To Page', URI.join(#site.boss_organization.admin_app_root_url, 'rostering').to_s, target: '_blank' %></h3>
<% end %>
</div>
This works if:
#site is available in the template (from your code it is not clear where it is loaded)
You do not need to run additional checks/tracking on the server side
Related
I am trying to incorporate AJAX into my rails application when a user clicks the 'add to order' button. I want the item to get added to the order without the page reloading. When I click 'add to order', the ajax request gets sent multiple times resulting in many items being added to my order instead of just 1 item.
Here is my link_to tag that triggers an ajax request:
<%= link_to ' ', :method => :post, :remote => true %>
Here is the code for my 'products' controller and the 'buy' action:
def buy
respond_to do |format|
format.html { redirect_to ' ' }
format.js
end
From my understanding, the default rails action will be to load the app/views/products/buy.js.erb file and execute the commands in that file.
Here is what I have in that file:
$("#order-panel").html("").append("<%= j render 'order_summary' %>");
I am essentially overriding the previous html with the new template in this file:
_order_summary.html.erb
This partial contains the following code:
<div class="panel-heading">
<span class='glyphicon glyphicon-tags'></span><span class="text">Order Summary</span>
</div>
<div class="panel-body">
<% if current_order %>
<% if current_order.total_items == 0 %>
<div class="order-summary-text empty-order">Your order is empty.</div>
<% else %>
<div class="order-summary-text">yadayada</div>
<div class="order-btns">
<%= link_to ' ' %>
<%= link_to ' ' %>
</div>
<% end %>
<% end %>
Does anyone know why my multiple items are getting added instead of 1?
Thanks in advance!
I have a page that has a form with one select list and a button.
<%= form_tag("/restaurant", method: "get") do %>
<%= collection_select(:company_name, nil, Food.distinct_company_names, :company_name, :company_name) %>
<%= submit_tag("Search One", :name => nil, :class=>"btn btn-search") %>
<% end %>
This works fine to pass the parameters to my restaurant page, but I don't want the URL to display like this-
http://localhost:3000/restaurant?utf8=%E2%9C%93&company_name%5B%5D=McDonalds
I want it to display like this (my routes are setup to allow it)-
http://localhost:3000/restaurant/McDonalds
Here's my current solution in the controller...
def restaurant
if params[:company_name]
#company_name = params[:company_name][0]
redirect_to("/restaurant/"+#company_name)
else
#avoid redirect loop
#company_name ||= params[:id]
end
end
I'm thinking there's an easier or more elegant way to do this, though. Can someone point me in the right direction?
You can use javascript to change the url when the select changes value. No need for the form.
<%= collection_select :company, :name, Food.distinct_company_names, :company_name, :company_name %>
<script>
$('#company_name').on('change', function() {
if ($(this).val().length) {
window.location = '/restaurant/' + $(this).val()
}
})
</script>
Instead of GET method you can use POST method to submit the form. Here you have to use restaurant controller and the action name is mcdonalds
<%= form_tag("/restaurant/mcdonalds", method: "post") do %>
<%= collection_select(:company_name, nil, Food.distinct_company_names, :company_name, :company_name) %>
<%= submit_tag("Search One", :name => nil, :class=>"btn btn-search") %>
in the routes.rb
post '/restaurant/mcdonalds'
My link_to looks like this:
<%= link_to image_tag(user_likes_selection.page_picture, :image_id =>
user_likes_selection.id, :controller => :preferences_controller,
:action => :checked_average_with_profile) %>
My controller, preferences_controller, has a method called checked_average_with_profile, which, as far as I can tell, is not being called when I click the image.
The html code that is generated from the link_to is
<img>
<a href="/preferences"><img action="checked_average_with_profile" alt="Soul_surfer_film"
controller="preferences_controller" height="70%" image_id="3254"
src="/assets/soul_surfer_film.jpg" width="70%" /></a>
</img>
Why isn't the controller code executed when the image is clicked?
in cases like these, it's easier to read the code if you use the block form of link_to
<%= link_to { :image_id => user_likes_selection.id, :controller => :preferences, :action => :checked_average_with_profile } do %>
<%= image_tag(user_likes_selection.page_picture %>
<% end %>
in your routes, you can also pass an as option so you can use a named route. assuming your routes looks like
match '/preferences/checked_average_with_profile/:image_id' => 'preferences#checked_average_with_profile', as: :check_average_profile
you can simplify your link using
link_to image_tag(user_likes_selection.page_picture), check_average_profile_path(user_likes_selection.id)
Here is how i do in my code.
<%=link_to(image_tag(user_likes_selection.page_picture), check_average_profile_path(user_likes_selection.id)) %>
Try:
<%= link_to image_tag(user_likes_selection.page_picture), url_for({:controller => 'preferences_controller', :action => 'checked_average_with_profile', :image_id => user_likes_selection.id}) %>
Put your paren after user_likes_selection.id, not at the end. You're mixing image tag properties with your link_to properties.
Try:
<%= link_to image_tag(user_likes_selection.page_picture, :image_id =>
user_likes_selection.id), {:controller => :preferences,
:action => :checked_average_with_profile} %>
Finally solved my problem by adding a collection with my action in resources:
resources :preferences do
collection do
get 'save_new_scores_to_profile'
get 'checked_average_with_profile'
end
end
Then, I modified my view code so that I could pass the image_id variable along to the controller.
<%= link_to image_tag(user_likes_selection.page_picture,
checked_average_with_profile_preferences_path(:image_id =>
user_likes_selection.id) %>
In my controller, I made sure to grab the image_id with params and put a redirect_to at the end:
def checked_average_with_profile
params[:image_id]
redirect_to preferences_url
end
If you have this problem, the key parts are passing the id (whatever that may be) within parenthesis of the controller path you specify and using a COLLECTION instead of a MEMBER in your routing file.
Anyone know if you can assign a tag and class to the disabled or current link? The example below only displays as plain text in the browser for the current link.
I have a bit of rails code displaying a list of buttons for each design in the database.
<% #id_cards.each do |id| %>
<%= link_to_unless_current id.design_type, id_card_design_path(id.id), :class => 'btn' %>
<% end %>
The active links are assigned the correct class and display as buttons.
link_to_unless_current accepts a block which can be used to override the default behavior.
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_unless_current
<%=
link_to_unless_current("Comment", { :controller => "comments", :action => "new" }) do
link_to("Go back", { :controller => "posts", :action => "index" })
end
%>
In the example above it would yield the 'Go back' link if the current page was the 'new comment' page.
#James gave proper answer its just you are too young to take it right :)
<% #id_cards.each do |id| %>
<%=
link_to_unless_current(id.design_type, id_card_design_path(id.id), :class => 'btn') do
content_tag(:p, id.design_type, :class => :some_class
end
%>
<% end %>
How do I place a link at the top of my page when the URL that it is pointing to is not determined until later down the page. In this example, I want to move Create and Edit Scenario links to the top of the page, but as you can see Edit Scenario depends on knowing the #scenario_id first.
<%= will_paginate #scens, :next_label => 'Older', :prev_label => 'Newer' %>
<div class="box">
<% for scenario in #scens %>
<% #created = scenario.created_at %>
<% #updated = scenario.updated_at %>
<% #scenario_id = scenario.id %>
<% if scenario.scenario_image.exists? %>
<%= scenario_image_tag(scenario) %>
<% end %>
<%= simple_format(scenario.description) %>
<% end %>
</div>
<% if session[:role_kind] == "controller" %>
<p>
<%= button_to "Create new scenario", :action => "create" %>
<% if #scens.size > 0 %>
<%= button_to "Edit scenario", :action => "edit", :id => #scenario_id %>
<% end %>
</p>
You can add the link at the top but you will need to programmatically access it later and then assign the URL to it. That needs some kind of reference or look-up capability, I'm thinking client-side javascript but that's as I don't know Ruby.
Alternatively you could create the link later when you have the URL and place the link at the top using CSS positioning. The actual position of all the DOM elements on the page need not match the order in which they are rendered.
One way to do this is to use a helper:
In your helper.rb file:
def stack_example(scens, &block)
html = 'Scenario Details'
edit_link = 'Edit Link'
yield html, edit_link
end
Then in your partial you could have something like:
<% stack_example(#scens) do |html, edit_link| %>
<%= edit_link %><br>
<%= html %>
<% end %>
Should output the following:
Edit Link
Scenario Details
I don't get it. Why do you create model in the view layer? Why wouldn't you create the model variables in the controller? Sth like:
class your_controller
def your_method
#scenario_id = ...
end
end
I think that your problem lays in the invalid MVC usage. Don't you think that all the #member #variables should be initialized before the view starts to render?