coordinates GET /coordinates(.:format) coordinates#index
POST /coordinates(.:format) coordinates#create
new_coordinate GET /coordinates/new(.:format) coordinates#new
edit_coordinate GET /coordinates/:id/edit(.:format) coordinates#edit
coordinate GET /coordinates/:id(.:format) coordinates#show
PUT /coordinates/:id(.:format) coordinates#update
DELETE /coordinates/:id(.:format) coordinates#destroy
tweets_search GET /tweets/search(.:format) tweets#search
tweets_index GET /tweets/index(.:format) tweets#index
class TweetsController<ApplicationController
def index
#include 'coordinates_controller.rb'
include SearchHelper
include ParamasHelper
#sql=a.search
#tweets=Tweets.paginate_by_sql(sql, :#page, :per_page => #per_page ).all
end
end
In my Rails app, I have two tables named Coordinates and Tweets. I have four actions to be rendered.
My routes.rb file
Tweetsblog2::Application.routes.draw do
resources :tweets, :coordinates
get "tweets/show"
get "tweets/index"
match "/tweets/show" => "tweets#show"
match "/tweets/index" => "tweets#index"
Whenever I navigate to http://localhost:3000/tweets, it's showing tweets/index instead of tweets/show and the same error I am getting with different names.
When I navigate to http://localhost:3000/tweets/show, it's giving ArgumentError in TweetsController#show.
When I navigate to http://localhost:3000/tweets/index its giving ArgumentError in TweetsController#show same thing
My code for show.html.erb:
<%= form_tag({controller: "tweets", action:"index" }, method: "get") do %>
<%= label_tag(:search, "search for:") %>
<%= text_field_tag(:show) %>
<%= text_field_tag(:search) %>
<%= submit_tag("get results ") %>
<% end %>
My code for index.html.erb:
<%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_id %></li>
<li><%= tweets.tweet_source %></li>
<li><%= tweets.tweet_text %></li>
<li><%= tweets.user_id %></li>
<li><%= tweets.user_name %></li>
<li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
<li><%= tweets.user_img %></li>
<li><%= tweets.longitude %></li>
<li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
<li><%= tweets.country %></li>
<% end %>
</ul>
It's not routing to the proper page. Please help me, I am stuck with this.
you have to write
resources :tweets, except: [:index,:show]
because you declared you resource first, so rails is trying to match its default routing instead of your custom action:
get "tweets/index"
-updating per comment-
Tweetsblog2::Application.routes.draw do
resources :coordinates
get "tweets/show" => "tweets#show"
get "tweets/index" => "tweets#index"
Removing the resources :tweets should fix your issue and allow you to use just call the 2 options. The initial resources :tweets tells rails that you wanted resourceful routes (index shows all of the resource, show a specify one, etc..). So just building out the 2 non-resourceful routes as above sounds like what you want. The way I normally do something like this is to just include the search form in in the index page and if there are no search params then show all tweets.)
Related
I am looking to pass this into the controller so I don't have to repeat or ask for path names.
<ul class="nav navbar-nav">
<li><%= link_to 'Breakfast', '/breakfast', class: ('active' if request.path == breakfast_path) %></li>
<li><%= link_to 'Lunch', '/lunch', class: ('active' if request.path == lunch_path) %></li>
<li><%= link_to 'Deli', '/deli', class: ('active' if request.path == deli_path) %></li>
<li><%= link_to 'Contact', '/contact', class: ('active' if request.path == contact_path) %></li>
</ul>
Gems will not be considered an answer...
Thanks!
I'm not sure passing it into the controller is a good solution - it's concerned with view logic so I think the view is the right level to handle it on. Perhaps creating a helper method that wraps link_to would be the right approach. Here's an idea without having tested it:
# YourHelper
def nav_link_to(label, path)
link_to(label, path, class: 'active' if path == request.path)
end
You could use the active_link_to gem:
<% food = %w(breakfast lunch deli contact) %>
<ul class="nav navbar-nav">
<% food.each do |meal| %>
<li><%= active_link_to meal.titleize, eval("#{meal}_path") %></li>
<% end %>
</ul>
I have the current code in my traders.index.html file
<ul>
<% #traders.each do |trader| %>
<li><%= link_to trader.name, trader %></li>
<%end%>
</ul>
I want to add an extra parameter to be sent through, I tried
<li><%= link_to trader.name, trader, {:restricted => params[:s]} %></li>
But this doesn't send the parameter, whats the actual format of the link_to to get this done?
You can do:
<%= link_to trader.name, trader_path(trader, restricted: params[:s]) %>
Need some help with this please.
I have a model Photos that has a column zone_id and category_id.
Photos with categories and Photos with views render the same index view (photos index).
I want to have a sidebar, depending on if you are in a Photo index by views or in a Photo index by category shows one dropdown or another.
So, if you click on Photo by Modern category, I want the sidebar to render a dropdown "Search by Zone"
In the other case, If you are in Photos index view by zone, I want my side bat to render a dropdown "Search by category".
I know this is with an if / unless, but don't know how to tell the conditional "Hey dude, Im in Photos by category, so you need to render Photos Zone dropdown".
Please help!
Photos Controller
def type
#photos = Photo.by_category(params[:category_id]).paginate(:page => params[:page])
render :index
end
def zone
#photos = Photo.by_zone(params[:zone_id]).paginate(:page => params[:page])
render :index
end
# I've got rid of Photo.with_user_avatar ...
def search
#photos = Photo.all
#photos = #photos.where('category_id = ?', params[:category_id]) if params[:category_id]
#photos = #photos.where('zone_id = ?', params[:zone_id]) if params[:zone_id]
#photos = #photos.paginate(:page => params[:page])
render :index
end
Routes
get 'spots/:category_id', to: "photos#type", as: :spots_category
get 'spots/zonas/:zone_id', to: "photos#zone", as: :spots_zone
get 'spots/:category_id/:zone_id', to: "photos#search", as: :spots_category_zone
get 'spots/zonas/:zone_id/:category_id', to: "photos#search", as: :spots_zone_category
Photos Index View
<% if #photos.first.zone %>
<h6 class="bold medium uppercase grey">Seleccionar estilo para: </br>
<span class="green big"><%= (#photos.first.zone.name ? "#{#photos.first.zone.name}": "") %></h6></span>
<!-- One dropdown-->
<ul class="listnone bold lightgrey">
<li><%= link_to "Moderno", spots_category_zone_path(1,#photos.first.zone) %>
<span class="spot_count small right"><%= #photos.count %></span></li>
<li><%= link_to "Clásico", spots_category_zone_path(2,#photos.first.zone) %>
<span class="spot_count small right"><%= #photos.count %></span></li>
<li><%= link_to "Industrial & Loft", spots_category_zone_path(3,#photos.first.zone) %>
<span class="spot_count small right"><%= #photos.count %></span></li>
<li><%= link_to "Minimalista", spots_category_zone_path(4,#photos.first.zone) %>
<span class="spot_count small right"><%= #photos.count %></span></li>
<li><%= link_to "Rústico", spots_category_zone_path(5,#photos.first.zone) %>
<span class="spot_count small right"><%= #photos.count %></span></li>
<li><%= link_to "Contemporáneo", spots_category_zone_path(6,#photos.first.zone) %>
<span class="spot_count small right"><%= #photos.count %></span></li>
<li><%= link_to "Étnico", spots_category_zone_path(7,#photos.first.zone) %>
<span class="spot_count small right"><%= #photos.count %></span></li>
<li><%= link_to "Art Deco", spots_category_zone_path(8,#photos.first.zone) %>
<span class="spot_count small right"><%= #photos.count %></span></li>
<li><%= link_to "Ecléctico", spots_category_zone_path(9,#photos.first.zone) %>
<span class="spot_count small right"><%= #photos.count %></span></li>
</ul>
<% else %>
<!-- The other dropdown -->
</i> Quiero ver spots de...<br>
<ul id="drop1" data-dropdown-content class="f-dropdown" data-options="align:right">
<li><%= link_to "Baños", spots_zone_path(1) %></li>
<li><%= link_to "Cocinas", spots_zone_path(2) %></li>
<li><%= link_to "Cuartos de estar", spots_zone_path(3) %></li>
<li><%= link_to "Dormitorios", spots_zone_path(4) %></li>
<li><%= link_to "Exteriores", spots_zone_path(5) %></li>
<li><%= link_to "Hostelería", spots_zone_path(6) %></li>
<li><%= link_to "Infantil", spots_zone_path(7) %></li>
<li><%= link_to "Oficinas", spots_zone_path(8) %></li>
<li><%= link_to "Salones", spots_zone_path(9) %></li>
</ul>
<% end %>
Thanks a lot!
Here we are again! Two easy ways.
You can set a variable equal to the action:
def type
#photos = Photo.by_category(params[:category_id]).paginate(:page => params[:page])
#action = 'type'
render :index
end
def zone
#photos = Photo.by_zone(params[:zone_id]).paginate(:page => params[:page])
#action = 'zone'
render :index
end
in your view
<% if #action == 'type' %>
<%= render 'sidebar/zone_menu' %>
<% elsif #action == 'zone' %>
<%= render 'sidebar/category_menu' %>
<% end %>
Or you can skip the variable and just call params[:action] in your view:
<% if params[:action] == 'type' %>
<%= render 'sidebar/zone_menu' %>
<% elsif params[:action] == 'zone' %>
<%= render 'sidebar/category_menu' %>
<% end %>
Personally, I'd go with the variable. It just feels like the kind of logic that belongs in the controller. Your mileage may vary.
I'd also put the menus into partials (make a "sidebar" folder, create _zone_menu.html.erb and _category_menu.html.erb respectively) and load them that way but you can certainly just put the full menus in the view instead. If you're going to do partials, I find it a best practice to keep instance variables out of partials by passing locals:
<%= render 'sidebar/category_menu', locals: { photo_count: #photos.count, current_zone: #photos.first.zone } %>
And then in the partial, replace #photos.count and #photos.first.zone with photo_count and current_zone.
As #subvertallchris pointed out you can access the params hash so I would do this in the controller.
before_filter :set_action
private
def set_action
#action = params[:action]
end
then you can use the views stated in #subvertallchris answer
In my rails application I need to display the matching tweets .lets say for example the matching results has 50 records, I need to display 10 records per page.I am getting the output which has all results but when I use pagination its showing link to different pages , but when I click the link to next page it says "string not matched". I tried different combinations like 5 per page ,but when I click the link to the next page it says "string not matched", but when I try without pagination it shows all the results
My code for the controller
class TweetsController<ApplicationController
def index
city = params[:show]
search_term = params[:text]
search_term[" "] = "%"
#tweets = Tweets.where("tweet_text LIKE? ", "%#{search_term}%").paginate( page: params[:page], per_page: 3)
My code for the view
<%= will_paginate #tweets %>
<% #tweets.each do |tweets| %>
<ul>
<li><%= tweets.id %></li>
<li><%= tweets.tweet_created_at %></li>
<li><%= tweets.tweet_source %></li>
<li><%= tweets.tweet_text %></li>
<li><%= tweets.user_id %></li>
<li><%= tweets.user_name %></li>
<li><%= tweets.user_sc_name %></li>
<li><%= tweets.user_loc %></li>
<li><%= tweets.user_img %></li>
<li><%= tweets.longitude %></li>
<li><%= tweets.latitude %></li>
<li><%= tweets.place %></li>
<li><%= tweets.country %></li>
<% end %>
</ul>
Anyone please help me with this
You have an error in
search_term[" "] = "%"
line. If it should replace whitespace with "%", it should be:
search_term.gsub!(/\s/, '%')
Right now I have a navigation partial that looks like this (x10 buttons)...
<% if current_controller == "territories" %>
<li><%= link_to "Territories", {:controller => 'territories'}, :class => 'active' %></li>
<% else %>
<li><%= link_to "Territories", {:controller => 'territories'} %></li>
<% end %>
<% if current_controller == "contacts" %>
<li><%= link_to "Contacts", {:controller => 'Contacts'}, :class => 'active' %></li>
<% else %>
<li><%= link_to "Contacts", {:controller => 'Contacts'} %></li>
<% end %>
Is there a more elegant/DRY solution for doing this?
In a similar vein to what Chuck said:
<% TARGETS.each do |target| %>
<li>
<%= link_to target.humanize,
{ :controller => target },
class => ('active' if current_controller == target)) %>
</li>
<% end %>
It's pretty easy to see where the repetition is in there. It's all of the general form:
<% if current_controller == XXXXX %>
<li><%= link_to XXXXX, {:controller => XXXXX}, CLASS %></li>
<% else %>
[do the same stuff minus ":class => 'active'"]
<% end %>
So we want XXXXX and CLASS to be variables (since those are the only things that change) and the rest can be a simple template.
So, we could do something like this:
%w(Contacts Territories).each |place|
<% class_hash = current_controller == place ? {:class => 'active'} : {}
<li><%= link_to place, {:controller => place}, class_hash)</li>
Check out rails-widgets on github. It provides a ton of convenience helpers for rails UI stuff (tabnavs, tooltips, tableizers, show hide toggle, simple css progressbar) in addition to navigation.
Here are the docs
Check out link_to_unless_current. Not exactly what you asked for, but it's close.
Also, you could put this kind of logic in a helper to abstract it out of the view.
Check out the simple-navigation plugin. It's an 'easy to use' rails plugin for creating navigations for your rails apps.
A slightly different version w/ link_to_unless_current:
<ul>
<% links.each do |link| -%>
<li><%= link_to_unless_current link.humanize, { :controller => target } %></li>
<% end -%>
</ul>
A good resource for stuff like this are the rails docs.