Routing error in Rails 3 - ruby-on-rails

I have the following in my view (index.html.erb) code:
<% #projects.each do |project| %>
<%= link_to (#project) do %>
<div id="tombstone">
...Some HTML here...
</div>
<% end %>
<% end %>
The goal is to have each project's synopsis displayed inside the tombstone DIV and have the entire DIV act as the link to the project's details page (show.html.erb).
My Controller has the following:
def show
#project = Project.find(params[:id])
end
and the routes has the following:
resources :projects do
...
resources :updates
end
The #project in the <%= link_to %> points back to the projects (index.html.erb) page, not to the project details page (show.html.erb) that the controller defines. I can only guess that the ID parameter isn't getting passed, but I can't figure out why.

<%= link_to(project) do %> # not #project
Technically you've been sent to index because #project probably new record. #project == Project.new, so link_to(Project.new) with GET request renders path to index action.

Related

(Rails) Trying to show the correct item when someone clicks on the link_to

Hello I'm still fairly new to Rails but, currently I have been working on a Rails project for bit now and my last issue with it is when someone clicks on a specific recipe it only shows the very first one a user ever created. I've accessed my database through my console to see if these recipes are saving and they are but when I click on any of the links to a specific recipe it still shows the incorrect one and it won't show the recipe name either.
Here's my recipe controller
class RecipesController < ApplicationController
before_action :require_login
def show
#recipe=Recipe.find_by(params[:name])
binding.pry
end
def index
#recipes =Recipe.all
#binding.pry
end
def new
#recipe = Recipe.new
#recipe.ingredients.build(name: "name")
end
def create
#recipe = Recipe.new(recipe_params)
#recipe.save
#binding.pry
redirect_to recipes_path
end
private
def recipe_params
params.require(:recipe).permit(:id,:name,:content, ingredients_attributes: [
:recipe_id,
:user_id,
:name,
:quantity
]
)
end
end
Index Page
<h1>All Recipes</h1>
<ul>
<% #recipes.each do |recipes| %>
<li><%= link_to recipes.name, recipes %></li>
<% end %>
</ul>
Show Page
<% #recipe.name do |r| %>
<h2> <%= r.name %></h2>
<h2> <%= r.content %></h2>
<%end%>
<h3>Ingredients</h3>
<ul>
<% #recipe.ingredients.each do |ingredient| %>
<li><%= "#{ingredient.name} X #{ingredient.quantity}" %></li>
<% end %>
</ul>
Any help would be appreciated
Thank you!
In your show method it's either one of those
Recipe.find_by(name: params[:name])
# or ...
Recipe.find(params[:id])
...depending on what setup you got going in your routes, the second one is the usual Rails way of doing things.
There are a few issues with your code. In your RecipesController, change the show action code to this:
def show
#recipe = Recipe.find(params[:id])
end
In your index.html.erb view, change the code that iterates through your recipes to this:
<% #recipes.each do |recipe| %>
<li><%= link_to recipes.name, recipe %></li>
<% end %>
And finally, in your show.html.erb view, change the code to this:
<h2><%= #recipe.name %></h2>
<h2><%= #recipe.content %></h>
<h3>Ingredients</h3>
<ul>
<% #recipe.ingredients.each do |ingredient| %>
<li><%= ingredient.name %> X <%= ingredient.quantity %></li>
<% end %>
</ul>
Summary of the changes
In the show action of the RecipesController, you search for the recipe by the id passed in from the view. That id comes from this line:
<%= link_to recipe.name, recipe %>
recipe gets to_param called on it, which returns the id of that particular recipe which you then use in the show action of the RecipesController to find the correct recipe.
In the index.html.erb view, you iterate through all of the recipes, via the #recipes variable, and output each recipe. Since you are outputting each recipe, you normally use recipe instead of recipes as the block variable.
In the show.html.erb view, you don't need to iterate through all recipes because you only have one recipe from the show action of the RecipesController. That recipe is stored in the #recipe variable, so you can use that variable directly in the view.

How to use one layout by two different actions in rails 4

I'm building a micropost rails app where users can create posts, I created a route and an action to display posts that belong to the signed-in user, happens that the general index layout for posts is exactly the same as the "myposts" layout so instead of duplicating code I would like to use just one layout with different parameters.
This is what I have until now:
routes.rb
resources :posts
get '/myposts', to: 'posts#my_posts', as: 'myposts'
posts_controller.rb
def index
#posts = Post.all
end
def my_posts
#myposts= Post.where(user_id: current_user.id)
end
index.html.erb
<% #posts.each do |post| %>
<div>
<h1><%= link_to post.title, post %></h1>
<%= link_to image_tag(post.meme_url(:thumb)), post, :target => "_blank" %>
</div>
<% end %>
my_posts.html.erb
<% #myposts.each do |post| %>
<div>
<h1><%= link_to post.title, post %></h1>
<%= link_to image_tag(post.meme_url(:thumb)), post, :target => "_blank" %>
</div>
<% end %>
Thanks in advance!
You can use 'render' on 'my_posts' action - http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
Add before 'end' in my_posts action:
render :index

Try to modify the "Todo app"

I'm starting on ruby on rails, first thing I want to try is modifying the "Todo app" example.
I do this by the tutorial: https://www.youtube.com/watch?v=fd1Vn-Wvy2w
After I finished, I saw that when I clicked on a todo_list, it will redirect to "show" form todo_list, but now I want the "show" display on the index with all the todo_list. I have tried to write
<%= todo_items.content %>
on the index page but it got some errors. Is there any solution or should I modify something on the Controller page so that
todo_items.content
should be able to display on the Index page
You need to load those items in your controller action first:
def index
#todo_items = TodoItem.all
end
then in index.html or whatever template you are rendering for this action you can render this collection:
<%= render #todo_items %>
that should render an todo_item partial that you should have created based on your video located at /app/views/todo_items/_todo_item.html.erb. Or you can do:
<% #todo_items.each do |item| %>
<%= item.content %>
<% end %>
In controller:
def index
#todo_lists = TodoList.all
end
in view:
<% #todo_lists.each_with_index do |list, index| %>
List <%= index + 1 %> todos:
<%= render list.todo_items %>
<% end %>

Interpolating data in a rails view

I'm a beginner at rails and thus far interplating data in views has been pretty straight forward. I've been introduced to something slightly new as far as how the controllers are setup and as a result I'm not sure how one would go about getting the data to present in the view.
First controller
class PagesController < ApplicationController
def index
#guestbook_entry = GuestbookEntry.new
render "welcome"
end
end
Second controller
class GuestbookEntriesController < ApplicationController
def create
GuestbookEntry.create(guestbook_entry_params)
redirect_to root_path, notice: "Thank you for your entry."
end
private
def guestbook_entry_params
params.require(:guestbook_entry).permit(:body)
end
end
And here is the welcome.html.erb
<h1>Welcome to My Guestbook</h1>
<br>
<%= image_tag("under_construction.gif") %>
<div id="guestbook-entries">
<p>Guestbook Entries:</p>
<ul>
</ul>
</div>
<%= form_for #guestbook_entry do |f| %>
<%= f.label :body, "Guestbook Entry:" %>
<%= f.text_area :body %>
<%= f.submit "Submit" %>
<% end %>
So it wants me to iterate through all the entries and display them on a welcome page that's located in view/pages/welcome.html.erb.
Up to this point I guess I've only been doing basic simple rails applications where the view corresponded with the controller, and followed the typical CRUD setup, where index would hold the #xxx = Xxxx.all and new/create would handle #xxx = Xxxx.new/create/build. I thought I could simply move the PageController's index action to create/new and do
def index
#guestbook_entry = GuestbookEntry.all
render "welcome"
end
To satisfy the test (it looks for render welcome in the index action)
This seems weird but again I admit, I'm a beginner.
If you want to list all the guest book entries on your root page you would do something like:
class PagesController < ApplicationController
def index
#guestbook_entry = GuestbookEntry.new
#guestbook_entries = GuestbookEntry.limit(10).all
render "welcome"
end
end
And in your view you would list them like:
<% if #guestbook_entries.any? %>
<div id="guestbook-entries">
<p>Guestbook Entries:</p>
<% #guestbook_entries.each do |entry| %>
<ul>
<li class="entry"><%= h(entry.body) %></li>
</ul>
<% end %>
</div>
<% end %>
The rest of you application is correct - you should be creating entries in GuestbookEntriesController#create. In many real life applications then the functionality of the standard new and edit actions can actually be a totally different controller.

Incorrect partial being rendered in my rails app

I've been stumped as to why my rails app is rendering the wrong partial. I have two partials, each related to a different controller (invitations and guests controllers). One partial lists the number of invitations sent out to users and the second partial lists those users who have confirmed their invitation. In addition, the second partial also allows one to see a simple profile of the confirmed guests.
What is happening is that when I visit the link related to the guests controller events/1/guests/, I expect to see the partial related to the guest profile. Instead, the partial related to the invitations controller is rendered. Both the invitations and guests controllers are nested resources of events.
Below is the code that I have been working with. Thanks!
Routes
resources :events, only: [:new, :show, :create] do
resources :invitations, only: [:new, :create, :show]
resources :guests, only: :show
end
match '/events/:id/guests', to: 'guests#show'
Guests controller
def show
#event = Event.find_by_id(params[:id])
#guestlist = #event.invitations.where(accepted: true)
end
views/guests/show.html.erb
<% provide(:title, #event.eventname + " Guest List") %>
<h1> Guest list for <%= #event.eventname %> </h1>
<% if #event.invitations.any? %>
<%= render #guestlist %>
<% end %>
views/guests/_guestlist.html.erb
<li>
<%= guestlist.name %> | <%= guestlist.occupation %> |
<%= guestlist.interests %>
</li>
Instead, the following partial is being rendered:
views/invitations/_invitation.html.erb
<li>
<%= invitation.name %> | <%= invitation.email %> |
<% if invitation.accepted == true %> <%= "Confirmed" %> <% else %> <%= "Pending" %> <% end %>
</li>
The following snippet depicts the correct way to invoke your partial:
# app/views/guests/show.html.erb
<%= render :partial => 'guests/guestlist', :locals => {:guestlist => #guestlist} %>
Since you need access to the #guestlist instance variable in your partial, you'll need to pass it as a local. Then, in your partial, guestlist will be available as a local variable.
Then, within your partial, you'll need to iterate over the members of your guestlist:
# app/views/guests/_guestlist.html.erb
<% guestlist.each do |guest| %>
<li>
<%= guest.name %> | <%= guest.occupation %> | <%= guest.interests %>
</li>
<% end %>
UPDATE:
The reason the OP's original invocation of the partial rendered the invitation partial is that #guestlist is actually comprised of Invitation objects, and thus, the <%= render #guestlist %> method was actually looking for a partial named invitation. From the canonical Rails guides:
There is also a shorthand for this. Assuming #products is a collection
of product instances, you can simply write this in the index.html.erb
to produce the same result:
<h1>Products</h1>
<%= render #products %>
Rails determines the name of the partial to use by looking at the
model name in the collection.
Because of this, you need to explicitly declare the name of the partial you want to use, otherwise ActionView will use the invitation partial by default.
If #guestlist is an object of type Guest, then it would by default render _guest.html.erb.
So, you can try This
<%= render 'guestlist' %>
Variable #guestlist would be automatically available in the partial, so no need to pass it in locals.
Hope this works.
In your show action, you have defined
#guestlist = #event.invitations.where(accepted: true) # it returns array of obejects or records
Now, Please have a try with the following code
views/guests/show.html.erb
<% unless #guestlist.blank? %>
<%= render "/guests/guestlist" %>
<% end %>
views/guests/_guestlist.html.erb
<% #guestlist.each do |guestlist| %>
<li>
<%= guestlist.name %> | <%= guestlist.occupation %> |
<%= guestlist.interests %>
</li>
<% end %>

Resources