Rails not passing id - ruby-on-rails-4

im new with rails. currently im facing problem with it. i have 3 model which is
order, task, and order task.
in the ordertask, i try to pass id from show.html to edit.html, but the rails only pass
http://localhost:3000/orders_tasks/%23%3COrderTask::ActiveRecord_Relation:0x00000009c9a078%3E/edit
instead of id number.
here is my show.html.erb
<p id="notice"><%= notice %></p>
<table class="table table-hover">
<tr>
<td>Task
</td>
<td>Status:
</td>
</tr>
<tr>
<% #status.each do |i| %>
<td><%= i.task_id %>
</td>
<td><%= i.status %>
</td>
</tr>
<% end %>
</table>
<%= link_to 'Edit', edit_orders_task_path(#status) %> |
<%= link_to 'Back', orders_tasks_path %>
here is my ordertask controller
class OrdersTasksController < ApplicationController
before_action :set_status, only: [:show, :edit, :update]
def index
#orders = Order.all
#status = OrderTask.includes(:task,:order).where(order_id: params[:id])
end
def edit
end
def show
end
def update
respond_to do |format|
if #status.update(order_params)
format.html { redirect_to #status, notice: 'Order was successfully updated.' }
format.json { render :show, status: :ok, location: #order }
else
format.html { render :edit }
format.json { render json: #status.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_status
#status = OrderTask.includes(:task,:order).where(order_id: params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order_task,:order).permit(:order_id,:status)
end
end
and here is my edit.html
<%= form_for(#status, html: {class: 'form form-horizontal'}).first.id do |f| %>
<% if #status.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#status.errors.count, "error") %> prohibited this order from being saved:</h2>
<ul>
<% #status.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<table class="table table-hover">
<tr>
<td><%= f.label "List of task" %>
</td>
<td><%= f.label "Status" %>
</td>
</tr>
<tr>
<td><%= f.task_id %>
</td>
<td><div class="dropdown">
<%= f.select(:status,['In progress', 'Completed'], {}, {class: "control"})%> </div>
</td>
</tr>
<tr>
<td>
</td>
<td><%= f.submit %>
</td>
</tr>
</table>
<% end %>
my order model
class Order < ActiveRecord::Base
belongs_to :staff
belongs_to :customer
belongs_to :service
has_many :order_task
has_many :tasks , through: :order_task
validates :staff_id , presence: true
end
my task model
class Task < ActiveRecord::Base
has_many :order_task
has_many :orders , through: :order_task
attr_accessor :status, :task_type
validates :task_name, presence: true, uniqueness: true
end
and lastly my order task model
class OrderTask < ActiveRecord::Base
self.primary_key = [:order_id]
self.table_name = "Orders_tasks"
belongs_to :order
belongs_to :task
end
i hope you guys can help me out cause i have stuck with this problem for a week now. :D

You should place the edit links in the loop... since #status contains multiple ordertaks, and each ordertask should have his own edit page:
<p id="notice"><%= notice %></p>
<table class="table table-hover">
<tr>
<td>Task
</td>
<td>Status:
</td>
<td>Action:</td>
</tr>
<tr>
<% #status.each do |i| %>
<td><%= i.task_id %>
</td>
<td><%= i.status %>
</td>
<td><%= link_to 'Edit', edit_orders_task_path(i) %></td>
</tr>
<% end %>
</table>
<%= link_to 'Back', orders_tasks_path %>
Edit: But I do like to point out that most of the time you're not in need of Controllers for your join models. So I do not know why you think you need it, most of the logic can be build from either the OrdersController or the TasksController.

Related

rails updating multiple rows in single form

i have a table with set of students, I want update attendance column to all the students with different values using single form which will save all the record.
note: i was able to save all the records individually.
here is my edit page
<table>
<thead>
<tr>
<th>Name</th>
<th>user_id</th>
<th>Present days</th>
<th>Total days</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #attendancce.each do |userr| %>
<tr>
<%= form_for(userr) do |f| %>
<td><%=userr.user.name %></td>
<td><%= userr.user_id %></td>
<td><%= f.number_field :present_days %></td>
<td><%= f.number_field :total_days %></td>
<td><%= f.submit %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "submit" %>
and below is my method for updating the records
def update
#attendancce = Attendance.all
respond_to do |format|
if #attendance.update(attendance_params)
format.html { redirect_to #attendance, notice: 'Attendance was successfully updated.' }
format.json { render :show, status: :ok, location: #attendance }
else
format.html { render :edit }
format.json { render json: #attendance.errors, status: :unprocessable_entity }
end
end
end
please suggest how to achieve my target

How to view and edit the uploaded excel sheets in rails using carrierwave gem (win7)

I am new to rails and wants to do following tasks with my uploader-
1) view the uploaded excel sheets in rails in editable mode
2)save that edited sheet in database(sqlite3).
I am seriously not getting what to do with this "view method"(in controller) to get my solution that's why leaving view method empty.
Also, please explain what is "resume.attachment_url" doing in
<%= link_to "Download Resume", resume.attachment_url %> in index.html.erb .
Thanks,
Aarzoo Goyal
routes.rb
Rails.application.routes.draw do
resources :resumes, only: [:index, :new, :create, :destroy]
root "resumes#index"
get 'resumes/index'
get 'resumes/new'
get 'resumes/create'
get 'resumes/destroy'
Model-Resume.rb
class Resume < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader # Tells rails to use this uploader for this model.
validates :name, presence: true # Make sure the owner's name is present.
validates :attachment, presence: true
end
resume_controller.rb
class ResumesController < ApplicationController
def index
#resumes = Resume.all
end
def new
#resume = Resume.new
end
def create
#resume = Resume.new(resume_params)
if #resume.save
redirect_to resumes_path, notice: "The resume #{#resume.name} has been uploaded."
else
render "new"
end
end
def view
end
def destroy
#resume = Resume.find(params[:id])
#resume.destroy
redirect_to resumes_path, notice: "The resume #{#resume.name} has been deleted."
end
private
def resume_params
params.require(:resume).permit(:name, :attachment)
end
end
new.html.erb
<% if !#resume.errors.empty? %>
<div class = "alert alert-error">
<ul>
<% #resume.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class = "well">
<%= form_for #resume, html: { multipart: true } do |f| %>
<%= f.label :name %>
<%= f.text_field :name %><br />
<%= f.label :attachment %>
<%= f.file_field :attachment %><br />
<%= f.submit "Save", class: "btn btn-primary" %>
<% end %>
</div>
index.html.erb
<% if !flash[:notice].blank?%>
<div class="alert alert-info">
<%= flash[:notice]%>
</div>
<%end%>
<br />
<%= link_to "New File", new_resume_path, class: "btn btn-primary"%>
<br />
<br />
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th>Download Link</th>
<th>View</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<% #resumes.each do|resume| %>
<tr>
<td><%= resume.name%></td>
<td><%= link_to "Download Resume", resume.attachment_url %></td>
<td><%= link_to "View "%></td>
<td><%= button_to "Delete", resume, method: :delete, class: "btn btn-danger", confirm: "Are you sure to delete #{resume.name}?" %></td>
</tr>
<% end %>
</tbody>
</table>

Why doesn't article.tag_list display the tags?

I have added gem 'acts-as-taggable-on' -v 2.3.1 in Gemfile.
Rails version 3.2.13, Ruby -v 1.9.3
In article.rb,
class Article < ActiveRecord::Base
acts_as_taggable_on :tags
has_many :comments, dependent: :destroy
validates :title, presence: true,
length: { minimum: 5 }
end
In articles_controller.rb,
class ArticlesController < ApplicationController
before_filter :authenticate_author!, except: [:index, :show]
def index
myarray = Article.all
#articles = Kaminari.paginate_array(myarray).page(params[:page]).per(10)
end
def show
#article = Article.find(params[:id])
end
def new
#article = Article.new
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def edit
#article = Article.find(params[:id])
end
def update
#article = Article.find(params[:id])
if #article.update_attributes(article_params)
redirect_to #article
else
render 'edit'
end
end
def destroy
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text, :tag_list => [])
end
end
In articles/_form.html.erb
<div class="field">
<%= f.label :tag_list, "Tags (separated by commas)" %><br />
<%= f.text_field :tag_list %>
</div>
I ran the following commands after installing the acts-on-taggable-gem,
rails generate acts_as_taggable_on:migration
rake db:migrate
In articles/index.html.erb
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= article.tag_list %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<% if author_signed_in? %>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<% end%>
</tr>
<% end %>
The tags are not being displayed in the index page.
But if I do ,
article = Article.new(:title => "Awesome")
article.tag_list = "awesome, cool"
article.save
The transaction gets committed and the tags are displayed on the browser.
Why aren't the tags getting saved and displayed in the index page?
The Tag List is an array, which means you need to 'loop' to extract the data. Whenever I have used acts_as_taggable I run through a loop to extract the tags. Something like the following should work:
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td>
<% article.tag_list.each do |tag| %>
<%= tag %>
<% end %>
</td>
<td><%= link_to 'Show', article_path(article) %></td>
<% if author_signed_in? %>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<% end%>
</tr>
<% end %>

Returning view to page I started from when deleting a user in a Rails view

Environment:
OS: Windows 8.1
Ruby: 2.1.5
Rails: 4.2
In my views/users/index.html.erb, I have:
<h1>Listing Users</h1>
<span class="eastdev_pagination">
<%= will_paginate #users, :container => false %>
</span>
<table class="table table-striped">
<thead>
<tr>
<th>First</th>
<th>Last</th>
<th>Work phone</th>
<th>City</th>
<th>Organization</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #users.each do |u| %>
<tr>
<td><%= u.first %></td>
<td><%= u.last %></td>
<% if u.work_phone %>
<% phone = number_to_phone(u.work_phone, area_code: true) %>
<td><%= phone %></td>
<% else %>
<td></td>
<% end %>
<td><%= u.city %></td>
<td><%= u.organization %></td>
<%= render partial: "layouts/show_edit_del_buttons", locals: {my_model: u} %>
</tr>
<% end %>
</tbody>
</table>
<span class="eastdev_pagination">
<%= will_paginate #users, :container => false %>
</span>
<br>
In my layouts/show_edit_del_buttons.html.erb, I have:
<td>
<%= link_to my_model, class: 'btn btn-info', title: 'View', 'data-toggle' => 'tooltip', 'data-placement' => 'right' do %>
<span class="glyphicon glyphicon-search"></span>
<% end %>
<td>
<% if staff_access_level? %>
<td>
<%= link_to [:edit, my_model], class: 'btn btn-warning', title: 'Edit', 'data-toggle' => 'tooltip', 'data-placement' => 'right' do %>
<span class="glyphicon glyphicon-pencil"></span>
<% end %>
</td>
<td>
<%= link_to my_model, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger', title: 'Delete', 'data-toggle' => 'tooltip', 'data-placement' => 'right' do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>
</td>
<% end %>
In my controllers/users.rb, I have the following:
def index
if params && params.has_key?('search-form')
item = params['search-form']
#users = User.where("first = ? OR last = ? OR organization LIKE ? AND approved = 1", item, item, "%#{item}%").paginate(:page => params[:page], per_page: 5).order('last')
else
#users = User.where("approved = 1").paginate(page: params[:page], per_page: 5)
end
end
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
The issue I am having, is that when I delete a user, I am returned to the first page. For example, I am on page 5 of the pagination, and I delete a user here, I am returned to the first navigation page.
How do I get the view to go back to the page I was on prior to the deletion?
Notes:
If I try something like:
<%= link_to my_model(:page => params[:page]), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger', title: 'Delete', 'data-toggle' => 'tooltip', 'data-placement' => 'right' do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>>
I get an error message:
Method not found: my_model
You should redirect user after delete on page you were on.
Look at your destroy method
redirect_to users_url
Here you should define a page while redirect.
Like this
my_model = Your path, like user_path(user, page: params[:page])
<%= link_to user_path(user, page: params[:page]), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger', title: 'Delete', 'data-toggle' => 'tooltip', 'data-placement' => 'right' do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>
You will get current page number in side destroy method
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to params[:page] ? users_url(page: params[:page]) : users_url }
format.json { head :no_content }
end
end

View loop with nested params

Working on the Domain view. I would like to loop through and list all Domains, all controls and any goals that are under those controls. I cannot figure out how to loop through the controls goals. Current solution just loops through all the goals, not just the goals that we connected to the Control
class Domain < ActiveRecord::Base
has_and_belongs_to_many :controls
class Control < ActiveRecord::Base
has_and_belongs_to_many :domains
has_many :goals
class Goal < ActiveRecord::Base
belongs_to :control
def index #domain controller
#domains = Domain.all
#controls = Control.all
#goals = Goal.all
end
domain index view
<% #domains.each do |domain| %>
<tr>
<td><%= domain.domainName %></td>
<% domain.controls.each do |control| %>
<td><%= control.controlName %></td>
<% #goals.each do |t| %>
<td>SG<%= t.goalsNumber %>, </td>
<% end %>
<% end %>
This is what I came up with. Probably not the most efficient way to do this but it works for now
<% domain.controls.each do |control| %>
<td>
<h3><%= control.controlName %></h3>
</td>
</tr>
<% #goals.each do |goals| %>
<% if goals.control.controlName==c ontrol.controlName %>
<tr>
<td>SG
<%=g oals.goalsNumber %>
<%=g oals.goalsName %>
</td>
</tr>
<% else %>
<% end %>
<% end %>
<% end %>