rails4, NameError in Zic#index, routes - ruby-on-rails-4

undefined local variable or method `nov_tisk_revij_index' for #<#<Class:0x007f361551b858>:0x007f3615519c60>
Hey guys, I'm setiing up a rails app and can't figure out what's wrong. I used rails g controller nov_tisk_revij index show to create my views and controller and now I can't link to it. If you can spot what I'm doing wrong, I'd be very grateful, don't want to remake this as is took a fair amount of work.
rake routes returns:
Prefix Verb URI Pattern Controller#Action
nov_tisk_revij_index GET /nov_tisk_revij/index(.:format) nov_tisk_revij#index
nov_tisk_revij_show GET /nov_tisk_revij/show(.:format) nov_tisk_revij#show
zic_index GET /zic/index(.:format) zic#index
root GET / zic#index
my html:
<%= link_to 'Novi izvodi tiskanih revij', nov_tisk_revij_index %>
routes:
get 'nov_tisk_revij/index'
get 'nov_tisk_revij/show'
get 'zic/index'
I tried adding resources :nov_tisk_revij into routes, and it still didn't work, just created additional routes for views I do not have.
controller:
class NovTiskRevijController < ApplicationController
def index
end
def show
end
end
I'm using rails4 with ruby 2.

<%= link_to 'Novi izvodi tiskanih revij', nov_tisk_revij_index_path %>
works

Related

In Rails, how do I define a "_new_path"?

I’m using Rails 4. How do I define a “_new_path” in my routes.rb file? I’m tryhin got create an admin area where an admin can create objects. So I have this in my config/routes.rb file
get 'admin/index'
get 'admin/list'
get 'admin/add'
get 'admin/approve'
Then in my app/views/admin/_add.html.erb file, I have
<%= form_for #my_object do |f| %>
but when I visit my page, I get the error
undefined method `my_objects_path' for #<#<Class:0x007ffea5de2c80>:0x007ffea5dd9798>
Did you mean? my_objects_new_path
I want the form to submit to the “create” method in my controller. How do I set this up?
try this commande rake routes to show yours paths rake routes
admin_index GET /admin/index(.:format) admin#index
admin_list GET /admin/list(.:format) admin#list
admin_add GET /admin/add(.:format) admin#add
admin_approve GET /admin/approve(.:format) admin#approve
after in app/views/admin/_add.html.erb file
<%= form_for admin_add_path do |f| %>

Rails 4 search from "home page" to other models/controllers

UPDATE 4-6-2015:
Double checked here:
https://github.com/activerecord-hackery/ransack
to make sure I had everything right, aside from a few hiccups I think I do. I am not clear how or what automagic makes the f.submit whisk the user away to that model index with nothing in that model filled out, and the index action taking presidence (hence after doing a search takes me to the index view of the groups (what I am searching on) and shows everything from db as if the Group.all was called ignoring the search string (that I can see in the address bar) completely.
Really not sure what I am missing I am sure it something minusculey stupid on my part.
EDIT/UPDATE: So i got most of this working, the search screen is going to that controller. I am not sure what automagic is causing that, I thought my respond_to could trap and send back json so I could this with an ajax call and update the table that is in the main page. THEN if they click on that table it takes them to the edit form of that particular item (i.e. the edit of the correct model).
My visitor_controller.rb:
def index
#search = Group.ransack(name_cont: params[:q])
#groups = #search.result
respond_to do |format|
format.html
format.js
end
end
Adding the routes file too:
Rails.application.routes.draw do
resources :groups
get 'visitors', to: 'visitors#index'
root to: 'visitors#index'
end
So groups is a controller/model that I am trying to search on (one of many, will use the example in the answer to do many when I get the one just working first.)
So I am building an app, I found a "simple search" tutorial for rails 4:
http://www.stefanosioannou.com/rails-4-simple-search-form/
But I think that this assumes you to search from the view/model and all that of your controller. I.e.
I am in the groups view, I want to search my groups all through that controller.
Really what I am doing and finding so hard to do is have a welcome page, that has three search bars. Each bar searches a different model/view/controller.
The welcome page is called visitors and has its own controller, and view just no model.
So my visitors controller has this:
class VisitorsController < ApplicationController
def index
#groups = Group.search(params[:gsearch])
respond_to do |format|
format.html
format.js
end
end
end
I was just trying to get one search bar working for my Group model, so in my Visitors index.html.erb:
<div class="span12">
<h3>Welcome IPA of NM</h3>
<!-- search bar here -->
<% form_tag group_path, :method => 'get' do %>
<p>
<%= text_field_tag :gsearch, params[:search] %>
<%= submit_tag "Search Groups", :name => nil %>
</p>
<% end %>
</div>
This fails though as it says
No route matches {:action=>"show", :controller=>"groups"} missing required keys: [:id]
so here is my groups controller:
class GroupsController < ApplicationController
before_action :set_group, only: [:show, :edit, :update, :destroy]
def show
end
private
# Use callbacks to share common setup or constraints between actions.
def set_group
#group = Group.find(params[:id])
end
end
I am sure somehow the form code in the visitors index.html.erb is working with that group_path which means it is trying to do a show but has no ID yet when just starting? I really am lost, new to rails4 and not sure what I am missing here. I am guessing the convention is do all things from your model/view/controller not a cross views as I am trying to do here? So something I have to do extra to get around the convention or I am doing the whole thing wrong from the get go?
I would highly recommend Ransack for search queries in rails. You can view it here > https://github.com/activerecord-hackery/ransack and you can also watch an awesome rails cast by Ryan Bates over at http://railscasts.com/episodes/370-ransack
Ransack works across the board, you could even search all 3 models from the same form.
Using ransack, you would simply use:
<%= search_form_for #q do |f| %>
# Search if the models name field contains...
<%= f.label :name_cont %>
<%= f.search_field :name_cont %>
<%= f.submit %>
<% end %>
Then the search controller processes this into #results:
def index
#q = params[:q]
#search = Model.search(name_cont: #q)
#results = #search.result
end
Then you would loop through the results on your show/index page.
<% #results.each do |result| %>
<%= result.name
<% end %>
This would show all of your results on one search page. If you wanted to search multiple models, you can either have multiple forms assigning a different instance variable for each one eg: #q1 #q2 #q3 or, you could search them all from one model by using different instance variables for the search and results.eg
def index
#q = params[:q]
#firstsearch = User.search(username_cont: #q)
#secondsearch = Post.search(title_or_description_cont: #q)
#users = #firstsearch.result
#posts = #secondsearch.result
end
Remember to add the routes =)
get 'search', to: 'search#index'

trying to link image to specific model id in rails

I'm having a routing issue with an image. In my app I have images of items on the home page. I would like them to link to their image page.
Here is what my items controller looks like:
class ItemsController < ApplicationController
def show
#item = Item.find(params[:id])
end
end
This is what I have in my routes:
Rails.application.routes.draw do
resources :items
end
And this is what I have in the item partial:
<%= link_to(image_tag(item.image.url(:thumb)), item_path(:id)) %>
What I expected after reading the rails routing guide was that this would link to the item page for that image. Here is their example:
photo_path(:id) returns /photos/:id (for instance, photo_path(10) returns /photos/10)
I should also add that this is in my home page controller:
class StaticPagesController < ApplicationController
def home
#items = Item.where.not(category_id: 1)
end
However, that is not working. I've tried several different things, but all produce errors. Is there a simple way to do this?
The normal way to do what you want is this:
<%= link_to item_path(item) do %>
<%= image_tag(item.image.url(:thumb)) %>
<% end %>
You can just pass the instance of the item to item_path and also if you have complicated html for a link, it is usual to put it in a block for the link as shown here (with link_to something do).

Rails 4 update attribute (belongs_to) with click on link ( within dropdown list on show page)

In my app I have Issue model, which belongs_to Status model. What I need to do is to have dropdown list of links (or spans, doesn't matter) with Statuses Id's on Issue show page, so I could change status clicking on this (could be non-ajax and ajax).
I am rather new to Rails, so I do not know how to implement this, was thinking of few ways to do this, but non seems to be working.
Because you're new, I'll outline what I'd do for you (hopefully it will help):
#config/routes.rb
resources :issues do
get "change_status/:status_id", to: "issues#change_status"
end
#app/models/status.rb
Class Status < ActiveRecord::Base
has_many :issues
scope :not, ->(id) { where('statuses.id != ?', id) }
end
#app/controllers/issues_controller.rb
def show
#issue = Issue.find params[:id]
#statuses = Status.all.not(#issue.id)
end
def change_status
issue = Issue.find params[:id]
issue.status = params[:status_id]
issue.save if issue.status_id_changed?
redirect_to issue
end
#app/views/issues/show.html.erb
<%= #statuses.each do |status| %>
<%= link_to status.title, issue_change_status_path(#issue, status) %>
<% end %>
There's obviously some stuff to explain - if you need me to do that, let me know and I'll give you the details!
You can simply use the link_to rails helper like in the following code:
link_to "Change Status", my_path_to_update_status, remote: true, method: :put
This will send an AJAX PUT request (thank to the remote: true) to your my_path_to_update_status.
Then, you just have to update the status in the action corresponding to the path specified in the link_to helper tag.
More information about the link_to rails helper here

error getting path returned rails 4

I'm trying to access my page gallery/new via a link, so i have created this
<%= link_to 'New gallery' new_gallery_path %>
rake routes gives
gallery_index_path GET /gallery(.:format) gallery#index
POST /gallery(.:format) gallery#create
new_gallery_path GET /gallery/new(.:format) gallery#new
edit_gallery_path GET /gallery/:id/edit(.:format) gallery#edit
gallery_path GET /gallery/:id(.:format) gallery#show
PATCH /gallery/:id(.:format) gallery#update
PUT /gallery/:id(.:format) gallery#update
DELETE /gallery/:id(.:format) gallery#destroy
and within my routes i have
resources :gallery
My view at gallery/new is
<%= nested_form_for #gallery, :html => { :multipart => true} do |f| %>
--content here
<% end %>
whenever i click the link to view this page i get
undefined method `galleries_path
Can someone point out my mistake, please?
You've chosen the wrong name for your resources. It should always be pluralized:
resources :galleries
From this Rails will generate the plural and singular paths correctly. galleries_path for the index, gallery_path for show, etc etc.