Render partial scaffold in the index view - Rails - ruby-on-rails

I want exibe the action 'Show' of scaffold in my index using ajax but when i click in show dont appear nothing my files is:
finances_controller.rb
def show
#finance = Finance.find(params[:id])
end
_show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Date update:</b>
<%= #finance.updated_at %>
</p>
<p>
<b>Money:</b>
<%= #finance.money %>
</p>
<p>
<b>Entrance:</b>
<%= #finance.entrance %>
</p>
<p>
<b>Description:</b>
<%= #finance.description %>
</p>
<p>
<b>Situation:</b>
<%= #finance.situation %>
</p>
<p>
<b>Local:</b>
<%= #finance.local %>
</p>
<%= link_to 'Edit', edit_finance_path(#finance) %> |
<%= link_to 'Back', finances_path %>
my
index.html.erb modified the button show:
Bem vindo <%= session[:user_name]%>
<div id='list' align='left'>
<h1>Finanças - Hoje é dia <%= Date.today %></h1>
<%= link_to 'New Finance', new_finance_path %>
<table cellpadding="5" >
<tr>
<th>Data de Inserção</th>
<th>Caixa</th>
<th>Entrada</th>
<th>Descrição</th>
<th>Situação</th>
<th>Local</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #finances.each do |finance| %>
<tr>
<td><%= finance.created_at %></td>
<td><%= finance.money %></td>
<td><%= finance.entrance %></td>
<td><%= finance.description %></td>
<td><%= finance.situation %></td>
<td><%= finance.local %></td>
<td><%= link_to 'Show', finance,:action => 'show',:id => #finance , :remote =>:true %></td>
<td><%= link_to 'Edit', edit_finance_path(finance) %></td>
<td><%= link_to 'Destroy', finance, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
</div>
<div id='tools' align='right'>
<%if #finance %>
<%= render('show') %>
<% end %>
</div>
<%= link_to 'Logout', new_session_url,method: :delete %>
when i click in the show appear the url 'localhost:3000/finances/1' but dont happening nothing when i click this i want click and show my partial 'show' in the same page, i render the partial 'show' if especified the #finance ,
<%if #finance %>
<%= render('show') %>
<% end %>

try to use = render 'finances/show'

Related

rails ransack remote true with pagination

I've a controller for WeekDay. Then I tried to add RanSack in my project. It works good except when I tried to enable remote true, it shows only search form. AJAX call is being made, but nothing shows up.
Controller code
def index
#q = WeekDay.ransack(params[:q])
#week_days = #q.result().page(params[:page]).per(10)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #week_days }
end
end
index.html.erb
<div id="week_days"><%= render 'week_days' %></div>
_week_days.html.erb
<%= search_form_for #q,remote: true do |f| %>
<%= f.label :datum_gteq %>
<%= f.date_field :datum_gteq%>
<%= f.label :datum_lteq %>
<%= f.date_field :datum_lteq %>
<%= f.submit %>
<% end %>
<table>
<thead>
<tr>
<th><%=sort_link #q, :datum %></th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #week_days.each do |week_day| %>
<tr>
<td><%= l week_day.datum %></td>
<td><%= link_to 'Show', week_day %></td>
<td><%= link_to 'Edit', edit_week_day_path(week_day) %></td>
</tr>
<% end %>
</tbody>
</table>
<%= paginate #week_days %>
<br>
<%= link_to 'New Week Day', new_week_day_path %>
some idea from me is you split the _week_days into partial and then render part of data using ajax, I added new div with name display-area as target for ajax to render and you should create javascripts to render it without reload the page with escape javascripts (j)
_week_days.html.erb
<%= render "search" %>
<%= render "my_data" %>
_search.html.erb
<%= search_form_for #q,remote: true do |f| %>
<%= f.label :datum_gteq %>
<%= f.date_field :datum_gteq%>
<%= f.label :datum_lteq %>
<%= f.date_field :datum_lteq %>
<%= f.submit %>
<% end %>
_my_data.html.erb
<div class="display-area">
<table>
<thead>
<tr>
<th><%=sort_link #q, :datum %></th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #week_days.each do |week_day| %>
<tr>
<td><%= l week_day.datum %></td>
<td><%= link_to 'Show', week_day %></td>
<td><%= link_to 'Edit', edit_week_day_path(week_day) %></td>
</tr>
<% end %>
</tbody>
</table>
<%= paginate #week_days %>
<br>
<%= link_to 'New Week Day', new_week_day_path %>
</div>
write _weekdays.js.erb inside app/assets/javascripts
$('.display-area').html("<%= j render(partial: 'my_data') %>")

Views in ruby on rails

I am learning to code in ruby on rails environment. I created two models cars and products. I want to have a main page which have a link to cars and products. On clicking each of them they should display their own view pages. Below are view pages for cars and users respectively :
app/views/cars/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Cars</h1>
<table>
<thead>
<tr>
<th>Make</th>
<th>Color</th>
<th>Year</th>
<th colspan="24"></th>
</tr>
</thead>
<tbody>
<% #cars.each do |car| %>
<tr>
<td><%= car.make %></td>
<td><%= car.color %></td>
<td><%= car.year %></td>
<td><%= link_to 'Show', car %></td>
<td><%= link_to 'Edit', edit_car_path(car) %></td>
<td><%= link_to 'Destroy', car, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Car', new_car_path %>
<h4>Import that data!</h4>
<%= form_tag import_users_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import CSV" %>
<% end %>
</div>
app/views/users/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Users</h1>
<table>
<thead>
<tr>
<th>User</th>
<th>Steps</th>
<th>Distance</th>
<th>Minutes Exercised</th>
<th>Hours of Sleep</th>
<th>Calories Burned</th>
<th colspan="24"></th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.user %></td>
<td><%= user.steps %></td>
<td><%= user.distance %></td>
<td><%= user.exercise %></td>
<td><%= user.sleep %></td>
<td><%= user.calories %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New User', new_user_path %>
<div>
<h4>Import that data!</h4>
<%= form_tag import_users_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import CSV" %>
<% end %>
</div>
<%= form_tag import_users_path, multipart: true, class: 'form-inline' do %>
<div class="form-group">
<%= link_to "Export CSV", users_path(format: "csv"), class: 'btn btn-primary' %>
</div>
<% end %>
I don't know how to create single main page and provide links to these views. One example would help me. Thanks in advance guys.
Say suppose you have a main page named home
In your home.html.erb write
<%= link_to "cars" cars_path %>
<%= link_to "users" users_path %>
You probably should use rails scaffold to have a look at the functional pages generated by it at first.
Try this .......
<%=link_to "cars" cars_path(car) %>
<%=link_to "users" users_path(user) %>
This link will send you to show method of the controller (users/cars). Then you need to create a show page in your views/cars/show.html.erb and views/users/show.html.erb then you can display their own view/show pages.
Hope this will work for you.
You need to understand concept of routes in rails.
Run command rake routes in your project folder. You will see mapping like this.
Prefix Verb URI Pattern Controller#Action
companies GET /companies(.:format) companies#index
So according to routes, following link will lead to index action of companies controller.
<%= link_to 'Companies', companies_path%>
As you can observe, in companies_path, companies is prefix shown in routes.
For your specific case, following links will work. In your
home.html.erb
<%=link_to "All Cars", cars_path%>
<%=link_to "All Users", users_path%>
Remember there is comma between "All Users" and users_path. Because link_to is just a method with multiple arguments.
For more info,
http://apidock.com/rails/v4.0.2/ActionView/Helpers/UrlHelper/link_to

Rails syntax error : unexpected keyword_ensure, expecting end-of-input

I am a newbie in rails and i tried creating a forum application based on a tutorial. This is my forum page but i keep getting the error:
syntax error, unexpected keyword_ensure, expecting end-of-input
Extracted source (around line #33):
30
31 <p><% if admin? %><%= link_to "New Forum", new_forum_path %><% end %></p>
here is the forum index page which is throwing the error:
<% title "Forums" %>
<table>
<tr>
<th width="70%">Forum</th>
<th width="30%">Last Post</th>
</tr>
<% for forum in #forums %>
<tr>
<td><h4><%= link_to h(forum.name), forum_path(forum.id) %></h4>
<small><%= forum.topics.count %> topics</small><br />
<%=h forum.description %></td>
<td class="right">
<% if forum.most_recent_post %>
<%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %>
ago by
<%= link_to forum.most_recent_post.user.username, "/users/#{forum.most_recent_post.last_poster_id}" %>
<% else %>no posts<% end %>
</td>
<% if admin? %>
<td><%= link_to "Edit", edit_forum_path(forum) %>
<% end %></td>
<!-- <% end %> -->
<% if admin? %>
<td><%= link_to "Destroy", forum, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
<p><% if admin? %><%= link_to "New Forum", new_forum_path %><% end %></p>
<!-- <% end %> --> what is this doing? a html commented ERB tag will still evaluate. Remove it. if you want to comment ruby code use # instead, like <% #end %>
Properly formatted code goes a long way towards diagnosing problems like this (mismatch and the like). Try out the following:
<% title "Forums" %>
<table>
<tr>
<th width="70%">Forum</th>
<th width="30%">Last Post</th>
</tr>
<% for forum in #forums %>
<tr>
<td>
<h4><%= link_to h(forum.name), forum_path(forum.id) %></h4>
<small><%= forum.topics.count %> topics</small>
<br />
<%=h forum.description %>
</td>
<td class="right">
<% if forum.most_recent_post %>
<%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %>
ago by
<%= link_to forum.most_recent_post.user.username, "/users/#{forum.most_recent_post.last_poster_id}" %>
<% else %>
no posts
<% end %>
</td>
<% if admin? %>
<td><%= link_to "Edit", edit_forum_path(forum) %></td>
<td><%= link_to "Destroy", forum, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
</table>
<% if admin? %>
<p><%= link_to "New Forum", new_forum_path %></p>
<% end %>
all I could see wrong is that you set end before it should here
<% if admin? %>
<td><%= link_to "Edit", edit_forum_path(forum) %>
<% end %></td>
so try to move it like this
<% if admin? %>
<td><%= link_to "Edit", edit_forum_path(forum) %>
</td><% end %>
I think you have the order of opening and closing blocks jumbled up.
if, for are all opening blocks that have to be closed at the appropriate times.
The commented-out end tag that Benjamin mentioned is actually important but misplaced and has to go between your </tr> and </table> tags to close the for forum in #forums.
I prepared a modified version with some realignments, so I could make sense of it more easily. Haven't actually tested it, though.
<% title "Forums" %>
<table>
<tr>
<th width="70%">Forum</th>
<th width="30%">Last Post</th>
</tr>
<% for forum in #forums %>
<tr>
<td>
<h4><%= link_to h(forum.name), forum_path(forum.id) %></h4>
<small><%= forum.topics.count %> topics</small><br />
<%=h forum.description %></td>
<td class="right">
<% if forum.most_recent_post %>
<%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %>
ago by
<%= link_to forum.most_recent_post.user.username, "/users/#{forum.most_recent_post.last_poster_id}" %>
<% else %>
no posts
<% end %>
</td>
<% if admin? %>
<td>
<%= link_to "Edit", edit_forum_path(forum) %>
</td>
<% end %>
<% if admin? %>
<td><%= link_to "Destroy", forum, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
</table>
<p>
<% if admin? %>
<%= link_to "New Forum", new_forum_path %>
<% end %>
</p>

Image tag shows all columns of model

Yesterday i made some experience with carrierwav, all works fine but at the image tag where normally only the image should displayed rails also shows the hole model, with created_at etc. Here you cann see it!
So now my view:
<% #patient.treatments.each do |treatment| %>
<tr>
<td><%= treatment.category.try(:typ) %></td>
<td><%= treatment.content %></td>
<td><%= treatment.day %></td>
<td><div class="arrow"></div></td>
</tr>
<tr>
<td colspan="5">
<%= link_to 'Löschen', [treatment.patient, treatment],
:confirm => 'Sind sie sicher?',
:method => :delete %>
<%= treatment.paintings.each do |paint| %>
<%= image_tag paint.name %>
<% end %>
</td>
</tr>
<% end %>
The problem has to be in <%= image_tag paint.name %>
Remove the = from <%= treatment.paintings.each do |paint| %>, that is making you also print the treatment.paintings array.
<% treatment.paintings.each do |paint| %>
<%= image_tag paint.name %>
<% end %>

Proper arguments for initializing a model in a view?

My question template lists answers, and lets somone add a new answer for a question.
But I'm not sure where and how to initialize a new answer for this line:
<%= link_to 'New answer', new_question_answer_path(#question, Answer.new) %>
from the context below:
<p>
<b>Body:</b>
<%=h #question.body %>
</p>
<h1>Listing answers</h1>
<table>
<tr>
<th>Body</th>
</tr>
<% #question.answers.each do |answer| %>
<tr>
<td><%=h answer.body %></td>
<td><%= link_to 'Show', answer %></td>
<td><%= link_to 'Edit', edit_answer_path(answer) %></td>
<td><%= link_to 'Destroy', answer, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New answer', new_question_answer_path(#question, Answer.new) %>
<%= link_to 'Edit', edit_question_path(#question) %> |
<%= link_to 'Back', questions_path %>
If I initialize the new Answer like this:
<%= link_to 'New answer', new_question_answer_path(#question, Answer.new) %>
the URL of the next page is garbled with an inspect call to the new Answer.
Try this:
<%= link_to 'New answer', new_question_answer_path(#question) %>

Resources