'Nil' error when calling models from root - ruby-on-rails

I have a model called Museum and another model called Exhibtion. Museum is a parent of Exhibition.
I have created an adhoc controller in order to handle the root page.
routes file :
get 'welcome/index'
resources :museums
resources :museums do
resources :exhibitions do
resources :exhibitionimages
end
resources :museumimages
end
root 'welcome#index'
(please disregard museumimages and exhitionimages models)
I am trying to show all exhibitions on the root page as a table with links to show, edit, ...
here is the Welcome controller
class WelcomeController < ApplicationController
def index
#exhibitions = Exhibition.all
end
end
and the viewfile index.erb:
<h1>Hello, Rails!</h1>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th>Start</th>
<th>End</th>
<th>Link</th>
<th>Price</th>
</tr>
<% #exhibitions.each do |exhibition| %>
<tr>
<td><%= exhibition.title %></td>
<td><%= exhibition.text %></td>
<td><%= exhibition.start %></td>
<td><%= exhibition.end %></td>
<td><%= exhibition.linktoexhibition %></td>
<td><%= exhibition.price %></td>
<td><%= link_to 'Show', museum_exhibition_path(exhibition.museum, exhibition), method: :get %></td>
<td><%= link_to 'Edit', edit_museum_exhibition_path(exhibition.museum, exhibition), method: :get %></td>
<td><%= button_to 'destroy', museum_exhibition_path(exhibition.museum, exhibition), method: :delete %></td>
</tr>
Though i get an error with the links.
It seems that the museum_id is not passed to the form link_to helpers where it appears as 'NIL' and fails the link creation.
Those links work perfectly when used inside the museum controller (where the same table is shown with exhibitions belonging to the very parent)

Related

Rails link_to not working correctly

I am trying to put in a link_to on my table to go to the show action but it is putting the URL as /admin/vulnerabilities.object_id instead of /admin/vulnerabilities/object_id
my index view is:
...
<% #vulnerabilities.each do |vulnerability| %>
<tr>
<td><%=link_to vulnerability.id, admin_vulnerabilities_path(vulnerability) %></td>
<td><%= vulnerability.type %></td>
<td><%=h truncate(vulnerability.description, :length => 80) %></td>
<td><%= vulnerability.published %></td>
<td><%= vulnerability.modified %></td>
<td><%= link_to vulnerability.href, vulnerability.href , target: :_blank %></td>
</tr>
<% end %>
...
I have a show.html.erb template setup and my show action is as follows:
def show
#vulnerabilities = Vulnerability.find(params[:id])
end
From what I can see, this should work but when clicking the links it just redirects to the index page, effectively refreshing it and not using my show page at all.
It would be helpful if you added the relevant parts of your routes.rb file to your question, but I speculate that the problem is that admin_vulnerabilities_path(vulnerability) should be admin_vulnerability_path(vulnerability).
Also, as noted in the comments, it is probably better to use #vulnerability as your instance name since find will return a single record.

How Do I Get an Modified Column from a Rails Scaffold to Hold Information as the others?

I generated a scaffold for a To-Do List app, and I left out some columns to add later.
I ran the command to create a migration to add a new column named client and I changed my files so that it shows on the projects index and form, but when i enter something into the client field and submit, it doesn't save the information and remains blank..
Update 1:
Here is whats in my routes:
'
Rails.application.routes.draw do
root :to => 'projects#index'
resources :projects
end
'
Here is my index view:
'
<h1 id="title">Project List</h1>
<table>
<thead>
<tr id="headers">
<th>Title</th>
<th>Client</th>
<th>Description</th>
<th>Hours</th>
<th>Done</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody class="col-md-2" id="listItems">
<% #projects.each do |project| %>
<tr id="table">
<td><%= project.title %></td>
<td><%= project.client %></td>
<td><%= project.description %></td>
<td><%= project.hours %></td>
<td><%= project.done %></td>
<td><%= link_to " #{image_tag('show.png')}".html_safe, project, id:'showButton' %></td>
<td><%= link_to " #{image_tag('edit.png')}".html_safe, edit_project_path(project), id:'editButton' %></td>
<td><%= link_to " #{image_tag('destroy.png')}".html_safe, project, id:'destroyButton', method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Project', new_project_path, id:"new" %>
<footer id="footer">Copyright 2014 Kira Banks</footer>
'
To keep your application secured, Rails has a feature called Strong Parameters and the docs says:
It provides an interface for protecting attributes from end-user
assignment. This makes Action Controller parameters forbidden to be
used in Active Model mass assignment until they have been whitelisted.
So, basically you need to whitelist the new client attribute in the Projects controller by adding it to the list:
class ProjectsController < ApplicationController
# ...
# at the end of the file
private
def project_params
params.require(:project).permit(:title, :description, :hours, :done, :client)
end
end

how to fix the following error : undefined method `PUT' for #<ActionDispatch::Routing:: Mapper:0x3f8ee48> in rails 3.2?

I am currently trying to create a shopping cart for my rails application and once populated my cart should be saved in the session.
Everytime i try to run the server to test if working or not, it seems my routes are not working at all...
See below my routes.rb, my cart controller and my index.
Any help will be appreciated.
routes.rb
Sewingsupplies::Application.routes.draw do
get "cart/index"
get "cart/success"
PUT "/cart/:id" => "cart#add"
DELETE "/cart/:id" => "cart#remove"
DELETE "/cart" => "cart#clear"
POST "/cart/checkout"
devise_for :admins
resources :catalogues
devise_for :users
cart controller
def index
sessions[:cart] = {}
render 'cart/index'
end
def success
end
def add
#catalogue = Catalogue.find(params[:id])
cart = sessions[:cart]
cart = {:catalogue => #catalogue.name,category => #catalogue.category, :code => #catalogue.code , :colour=> #catalogue.colour, :description => #catalogue.description, :image => #catalogue.image , :unitprice => += #catalogue.unitprice, :unitquantity => +=1, :unitweight => += #catalogue.unitweight }
sessions[:cart] = cart
render 'cart/add'
end
end
and my index page
<h1>Listing catalogues</h1>
<table>
<tr>
<th>Name</th>
<th>Code</th>
<th>Category</th>
<th>Description</th>
<th>Unitprice</th>
<th>Unitquantity</th>
<th>Unitweight</th>
<th>Colour</th>
<th>Image</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
<% #catalogues.each do |catalogue| %>
<tr>
<td><%= catalogue.name %></td>
<td><%= catalogue.code %></td>
<td><%= catalogue.category %></td>
<td><%= catalogue.description %></td>
<td><%= catalogue.unitprice %></td>
<td><%= catalogue.unitquantity %></td>
<td><%= catalogue.unitweight %></td>
<td><%= catalogue.colour %></td>
<td><%= image_tag(catalogue.image, :width => 150) if catalogue.image.present?%></td>
<td><%= catalogue.user.email %></td>
<td><%= link_to 'Show', catalogue %></td>
<td><%= link_to 'Edit', edit_catalogue_path(catalogue) %></td>
<td><%= link_to 'Destroy', catalogue, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to "Add to cart", controller: "cart", action: "add", id: #catalogue.id, method: :post %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to "View cart", controller: "cart", action: "index" %>
<%= link_to 'New Catalogue', new_catalogue_path %>
In your routes.rb file you have defined the routes with PUT and DELETE in uppercase. Define the routes in lowercase like put and delete as these are methods of ActionDispatch::Routing::Mapper class which are called to setup routes for your application. As ruby is case sensitive you need to call the method with correct case.

Returning data from database to another page in rails

How can I return data from the database on another page?
I can file this under: views / posts / index.htm.erb
<h1>Listing posts</h1>
<table>
<tr>
<th>Titulo</th>
<th>Conteudo</th>
<th>Categoria</th>
<th>Criado em</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #posts.each do |post| %>
<tr>
<td><%= post.titulo %></td>
<td><%= post.conteudo %></td>
<td><%= post.category.name %></td>
<td><%= post.created_at.strftime("%d/%m/%Y") %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Delete', post, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Post', new_post_path %>
And I wanted to display these values ​​on another page: views / home / blog.html.erb
How do I do this? Could you explain all the steps so that I can display these values ​​in my other page.
Thanks Kocur4d
but how do I get only some information? eg: I would like that to appear in the title of the post page like this way (in blog.html.erb):
<div class="post-title">
<h2 class="title"> <a href="link_to_post"> **<% = post.titulo%>** </ a> </ h2>
</ div>
Step 1. Create controller
In your app root directory run:
rails g controller home blog
Modify controllers/homes_controller.rb :
class HomesController < ApplicationController
def blog
#posts = Post.all
end
end
Your controllers/posts_controller.rb should be already set up. Minimum what you need for your question is to have index method defined you might have other methods as well:
class PostsController < ApplicationController
def index
#posts = Post.all
end
end
Step 2. Extract Partial
change views/posts/index.htm.erb :
<h1>Listing posts</h1>
<%= render partial: 'shared/posts', object: #posts %>
<%= link_to 'New Post', new_post_path %>
create/modify views/home/blog.html.erb :
<h1>Listing posts</h1>
<%= render partial: 'shared/posts', object: #posts %>
<%= link_to 'New Post', new_post_path %>
create views/shared/_posts.html.erb :
<table>
<tr>
<th>Titulo</th>
<th>Conteudo</th>
<th>Categoria</th>
<th>Criado em</th>
<th></th>
<th></th>
<th></th>
</tr>
<% posts.each do |post| %>
<tr>
<td><%= post.titulo %></td>
<td><%= post.conteudo %></td>
<td><%= post.category.name %></td>
<td><%= post.created_at.strftime("%d/%m/%Y") %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Delete', post, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
Step 3. Set up routes.
You should have something like this in your routes.rb file:
resources :posts or match 'posts/index' => 'posts#index'
add this to config/routes.rb:
match 'home/blog' => 'home#blog'
so it might look like this(there is few variants):
config/routes.rb:
YourAppName::Application.routes.draw do
root to: 'posts#index'
resources :posts
match 'home/blog' => 'home#blog'
end
Now when you start rails server(assuming standard configuration) and visit:
127.0.0.1:3000/posts/index and 127.0.0.1:3000/home/blog
you should see same content.
This should work copy-and-paste but I could make some typos and other small mistakes(hope not, if ill find any ill try to edit them asap). In general look at it as you need 3 steps to forward http request down your rails application stack.
Map url to controllers using routes.
Create controllers and inside prepare data for views.
Display data in Views.
Look around in Rails Guides, Rails for Zombies and Rails Tutorial for more info.
---------Upadate to your second question-----------
I don't really understand what would you like to achieve?? At the moment both index.html.erb and blog.html.erb showing the same data, that was what you ware asking for?
post representing one post and is available in sharde/_posts.html.erb. You can't reference it from index.html.erb or blog.html.erb.
#posts represents all the posts and its available in index.html.erb or blog.html.erb.
render partial: 'shared/posts', object: #posts -- this line say "Hey man! Paste here content of shared/posts file, and btw I have here a local variable #posts so if you need to use that date in shared/posts file Ill name it posts from in side there"
To make them look different modify both files and part that will be identical for both of them is in a sharde/_posts.html.erb.
Try for example remove this line:
<td><%= post.category.name %></td>
from shared file to see what is going to happen.
Add some html tags and thinker with it.
Rails has may helper methods available' to find out about them check the links I give you and google, google, google.
Try to add some links with link_to helper
In your home controller, for the blog method, set #posts as you need...
Maybe
#posts = Post.all

rails "No route matches" even though it's in routes

I have this form:
<% #softwares.each do |l| %>
<tr>
<td><%= l.vendor %></td>
<td><%= l.title %></td>
<td><%= l.edition %></td>
<td><%= l.amount %></td>
<td><%= link_to 'view', software_path %></td>
<% end %>
When i click on the view link i get this error:
No route matches {:action=>"show", :controller=>"softwares"}
However when i run rake routes it does show up:
software GET /softwares/:id(.:format) softwares#show
and if i type it into the browser manually it works fine
Pass software object in path because it's a member route
<%= link_to 'view', software_path(l) %>
For RESTful resources you can just pass the resource:
link_to 'view', l
# => view

Resources