In Rails, how do I define a "_new_path"? - ruby-on-rails-4

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| %>

Related

Rails controller specific assets for selected controllers only

I want to use the controller specific assets only for some controllers, but not for all. Hence, I didn't add the assets for which I don't need separate files. But this leads to 404 for those resources as they are not present, e.g. my app/views/layouts/dashboard.html.erb file contains the following piece of code to generate the controller specific js/css:
<%= stylesheet_link_tag 'dashboard', params[:controller], media: 'all' %>
<%= javascript_include_tag 'dashboard', params[:controller] %>
However, I do not want to create separate files for Admin controller, as the admins page has no new css/js required. But, app tries to locate the assets for the controller and since not found it returns the 404 as follows:
GET http://dashboard.localhost.com:3000/stylesheets/dashboard/admins.css 404 (Not Found)
GET http://dashboard.localhost.com:3000/javascripts/dashboard/admins.js 404 (Not Found)
What should be done to prevent this?
Moreover, there are a few controllers for which the assets are obviously not required, e.g. Registrations controller, leading to 404 for http://0.0.0.0:3000/stylesheets/store/registrations.css for http://0.0.0.0:3000/account/edit page
Here is what I did to fix this in my application.html.erb:
<%= stylesheet_link_tag 'dashboard' %>
<%= stylesheet_link_tag params[:controller] if InmonarchWebsite::Application.assets.find_asset("#{params[:controller]}.css") %>
<%= javascript_include_tag 'dashboard' %>
<%= javascript_include_tag params[:controller] if InmonarchWebsite::Application.assets.find_asset("#{params[:controller]}.js") %>

want to get pagespeed in my view page on rails 4

I have implemented the google page speed on rails 4 with the help of pagespeed gem but I am getting my result on console. I want this result on my view page.
Controller code:-
#request = PageSpeed::Request::new("www.google.com", 'api key', 'desktop')
puts #request.pagespeed
The instance variable #request defined in your controller will be available in your views as
<%= #request %>
for pagespeed
<%= #request.pagespeed %>

rails4, NameError in Zic#index, routes

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

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).

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.