REFORMULATED FOR MORE INFO
I'll be rather short. As a newbie, that's the error I am getting while developing my rails app:
param is missing or the value is empty: task
The error highlights:
def task_params
params.require(:task).permit(:name, :description, :deadline, :status, :pdf, :done)
end
It happens when I click the button 'Mark as done' I'm creating.
Here follows the code:
app/views/tasks/index.html.erb:
<p id="notice"><%= notice %></p>
<h1>Tasks</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Time</th>
<th>Ready?</th>
<th colspan="10"></th>
</tr>
</thead>
<tbody>
<% #tasks.each do |task| %>
<tr>
<td><%= task.name %></td>
<td><%= task.description %></td>
.
.
.
<td><%= (link_to 'Mark done', task_path(task, done: true), method: :PUT) %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
app/controllers/tasks_controller.rb:
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
# GET /tasks
# GET /tasks.json
def index
#tasks = Task.all
end
# GET /tasks/1
# GET /tasks/1.json
def show
end
# GET /tasks/new
def new
#task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks
# POST /tasks.json
def create
#task = Task.new(task_params)
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: 'Task was successfully created.' }
format.json { render :show, status: :created, location: #task }
else
format.html { render :new }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tasks/1
# PATCH/PUT /tasks/1.json
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to #task, notice: 'Task was successfully updated.' }
format.json { render :show, status: :ok, location: #task }
else
format.html { render :edit }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1
# DELETE /tasks/1.json
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
#task = Task.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def task_params
params.require(:task).permit(:name, :description, :deadline, :status, :pdf, :done)
end
end
Thanks for the help!
since you've done this in controller as you said in command
before_action :set_task
def set_task
#task = Task.find(params[:id)
end
you should simply be able to do :
<td><%= 'Mark as done', tasks_setdone_path(task), method: :post %></td>
and not <td><%= 'Mark as done', tasks_setdone_path(task), method: :post %></td>
'#' represent instance variable accessible from controller AND template.
Another thing is that your action controller will require a respond (html or json).
Now that you answer is given, here is the proper way to do it.
task_path(#task, done: true), method: :PUT
POST is use for creation where PUT is use for updating an object.
Related
So my goal with this method is to have it link to customers/1/showcar similar to how it will link to customers/1/edit, which is how I'm attempting to model my code.
My controller is
class CustomersController < ApplicationController
before_action :set_customer, only: [:show, :edit, :update, :destroy, :showcar]
# GET /customers
# GET /customers.json
def index
#customers = Customer.all
end
# GET /customers/1
# GET /customers/1.json
def show
end
# GET /customers/1/showcar
def showcar
end
# GET /customers/new
def new
#customer = Customer.new
end
# GET /customers/1/edit
def edit
end
# POST /customers
# POST /customers.json
def create
#customer = Customer.new(customer_params)
respond_to do |format|
if #customer.save
format.html { redirect_to #customer, notice: 'Customer was successfully created.' }
format.json { render :show, status: :created, location: #customer }
else
format.html { render :new }
format.json { render json: #customer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /customers/1
# PATCH/PUT /customers/1.json
def update
respond_to do |format|
if #customer.update(customer_params)
format.html { redirect_to #customer, notice: 'Customer was successfully updated.' }
format.json { render :show, status: :ok, location: #customer }
else
format.html { render :edit }
format.json { render json: #customer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /customers/1
# DELETE /customers/1.json
def destroy
#customer.destroy
respond_to do |format|
format.html { redirect_to customers_url, notice: 'Customer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_customer
#customer = Customer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def customer_params
params.require(:customer).permit(:cust_id, :cust_fname, :cust_lname, :cust_phone, :cust_addr, :cust_date)
end
end
and my html.erb file where I'm attempting to call the method is
<style>
th, td{
padding-left: 20px;
}
</style>
<p id="notice"><%= notice %></p>
<h1>Customers</h1>
<table>
<thead>
<tr>
<th>Cust ID</th>
<th>Cust fname</th>
<th>Cust lname</th>
<th>Cust phone</th>
<th>Cust addr</th>
<th>Cust date</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #customers.each do |customer| %>
<% belongstocust = Car.where(cust_id: customer.cust_id) %>
<tr>
<td><%= customer.cust_id %></td>
<td><%= customer.cust_fname %></td>
<td><%= customer.cust_lname %></td>
<td><%= customer.cust_phone %></td>
<td><%= customer.cust_addr %></td>
<td><%= customer.cust_date %></td>
<td><%= link_to 'Show', customer %></td>
<td><%= link_to 'Edit', edit_customer_path(customer) %></td>
<td><%= link_to 'Destroy', customer, method: :delete, data: { confirm: 'Are you sure?' } %></td>
#placeholder, not permanent code
<% i = '' %>
<% belongstocust.each do |car| %>
<% i = car.car_model %>
<td><%= link_to 'Show ' + i, car_path(car) %></td>
<% end %>
<td><%= link_to 'Show Car', showcar_customer_path(customer) %> </td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Customer', new_customer_path %>
<br>
<%= link_to 'Home', home_index_path %>
The issue is, whenever I have the code <td><%= link_to 'Show Car', showcar_customer_path(customer) %> </td>, I get the noMethodError even though the method is defined inside the controller, and it looks the exact same as def show and def edit. I've tried making a controller called customer, and adding the showcar method to that controller, and it worked, but it wouldn't pass over the customer. I also tried adding showcar to customer.rb, but it also gave me a noMethodError. I'm a complete noob when it comes to ruby, and was just told to make a project using the framework, so I've been having to learn along the way. This could be a really simple issue that I don't know how to solve due to my ignorance, so if that's the case I'm sorry.
I get the noMethodError even though the method is defined inside the controller
No. You have showcar defined, not showcar_customer_path. You are missing a route, the thing that defines xxx_path methods.
In your config/routes.rb you probably have
resources :customers
To register this new action, you can do
resources :customers do
member do
get :showcar
end
end
Now showcar_customer_path should be available to use in the views.
i am learning rails and creating a web app which also got ecommerce in it
There is a Form which user can fill only if he is logged in, For that i was using Devise, then for e-commerce i installed Spree
Spree got its own login authentication, and there is no authenticate_user! in controllers too,
i removed devise and having a tough time finding how to use Spree's authentication with my Form
here is UPDATED Form's controller:
complaints_controller.rb
module Spree
class ComplaintsController < Spree::StoreController
before_action :require_login
before_action :set_complaint, only: [:show, :edit, :update, :destroy]
# GET /complaints
# GET /complaints.json
def require_login
redirect_to spree_login_path unless current_spree_user
end
def index
#complaints = Complaint.all
end
# GET /complaints/1
# GET /complaints/1.json
def show
end
# GET /complaints/new
def new
#complaint = Complaint.new
end
# GET /complaints/1/edit
def edit
end
# POST /complaints
# POST /complaints.json
def create
#complaint = Complaint.new(complaint_params)
respond_to do |format|
if #complaint.save
format.html { redirect_to #complaint, notice: 'Complaint was successfully created.' }
format.json { render :show, status: :created, location: #complaint }
else
format.html { render :new }
format.json { render json: #complaint.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /complaints/1
# PATCH/PUT /complaints/1.json
def update
respond_to do |format|
if #complaint.update(complaint_params)
format.html { redirect_to #complaint, notice: 'Complaint was successfully updated.' }
format.json { render :show, status: :ok, location: #complaint }
else
format.html { render :edit }
format.json { render json: #complaint.errors, status: :unprocessable_entity }
end
end
end
# DELETE /complaints/1
# DELETE /complaints/1.json
def destroy
#complaint.destroy
respond_to do |format|
format.html { redirect_to complaints_url, notice: 'Complaint was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_complaint
#complaint = Complaint.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def complaint_params
params.require(:complaint).permit(:id_society, :id_user, :heading, :text, :active, :action, :IsDelete, :flat_number)
end
end
end
<% end %>
index.html.erb
<% if spree_current_user %>
<p id="notice"><%= notice %></p>
<h1>Listing Complaints</h1>
<table>
<thead>
<tr>
<th>Id society</th>
<th>Id user</th>
<th>Heading</th>
<th>Text</th>
<th>Active</th>
<th>Action</th>
<th>Isdelete</th>
<th>Flat number</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #complaints.each do |complaint| %>
<tr>
<td><%= complaint.id_society %></td>
<td><%= complaint.id_user %></td>
<td><%= complaint.heading %></td>
<td><%= complaint.text %></td>
<td><%= complaint.active %></td>
<td><%= complaint.action %></td>
<td><%= complaint.IsDelete %></td>
<td><%= complaint.flat_number %></td>
<td><%= link_to 'Show', complaint %></td>
<td><%= link_to 'Edit', edit_complaint_path(complaint) %></td>
<td><%= link_to 'Destroy', complaint, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Complaint', new_complaint_path %>
<% else %>
<h1> please login</h1>
<% end %>
This works, as it verifies user's authentication in View, is there any way to check it in controller? Like if user is logged in it will be sent to action or else redirected to login?
Thank you
Spree uses devise authentication through a extension:
https://github.com/spree/spree_auth_devise
For authenticate your actions at controller(your own controllers) level, you need to define your own authentication filter. So you can manage something like this:
before_action :require_login
def require_login
redirect_to login_url unless current_spree_user
end
I'm new to Ruby on Rails and I started with a scaffold and added another model manually. I can't seem to get the values from the model I manually generated to display in my index view.
My first model is for Golf Courses names, city, par, and hole_id. The second model is the amount of holes for each course. For some reason I can't get the hole amount to display Below is my code.
Models
class Course < ActiveRecord::Base
has_many :holes
end
class Hole < ActiveRecord::Base
belongs_to :course
end
Controller
class CoursesController < ApplicationController
before_action :set_course, only: [:show, :edit, :update, :destroy]
# GET /courses
# GET /courses.json
def index
#courses = Course.all
#holes = Hole.all
end
# GET /courses/1
# GET /courses/1.json
def show
end
# GET /courses/new
def new
#course = Course.new
end
# GET /courses/1/edit
def edit
end
# POST /courses
# POST /courses.json
def create
#course = Course.new(course_params)
respond_to do |format|
if #course.save
format.html { redirect_to #course, notice: 'Course was successfully created.' }
format.json { render :show, status: :created, location: #course }
else
format.html { render :new }
format.json { render json: #course.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /courses/1
# PATCH/PUT /courses/1.json
def update
respond_to do |format|
if #course.update(course_params)
format.html { redirect_to #course, notice: 'Course was successfully updated.' }
format.json { render :show, status: :ok, location: #course }
else
format.html { render :edit }
format.json { render json: #course.errors, status: :unprocessable_entity }
end
end
end
# DELETE /courses/1
# DELETE /courses/1.json
def destroy
#course.destroy
respond_to do |format|
format.html { redirect_to courses_url, notice: 'Course was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_course
#course = Course.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def course_params
params.require(:course).permit(:name, :city, :hole_id)
end
end
View
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #course.name %>
</p>
<p>
<strong>City:</strong>
<%= #course.city %>
</p>
<p>
<strong>Hole:</strong>
<%= #course.holes %>
</p>
<%= link_to 'Edit', edit_course_path(#course) %> |
<%= link_to 'Back', courses_path %>
<%= #course.holes %> gives you an ActiveRecord_Associations_CollectionProxy
You need to ask for its size, length or its count to get the total amount of holes that belong to #course which means you have to say #course.holes.size, #course.holes.length or #course.holes.count. Check the documentation for the differences between these three.
Hi I need to create recurring event in rails. i.e) every monday i want to conduct meeting for 6months. so i want to make this event as recurrance event in calendar.as I am new to rails, i have followed some links. but i dont get any code or idea about how to do it. I have searched well in SO and got one link.
http://stackoverflow.com/questions/10148960/recurring-events-in-calendar-rails
But really dnt understand what they said. and also i have reviewed few suggested links but those are written in php. so i am unable to follow those links too. pls provide me some code to achieve this task. someone said we cannot achieve recurring event in fullcalendar. but i dont knw exactly whether it is right or wrong. if it is wrong then provide me some code to do this. or guide me if you have idea. thanks in advance.
This is my controller:
class EventsController < ApplicationController
before_action :set_event, only: [:show, :edit, :update, :destroy]
# GET /events
# GET /events.json
def index
#events = Event.all
end
# GET /events/1
# GET /events/1.json
def show
end
# GET /events/new
def new
#event = Event.new
end
# GET /events/1/edit
def edit
end
# POST /events
# POST /events.json
def create
#event = Event.new(event_params)
respond_to do |format|
if #event.save
format.html { redirect_to #event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: #event }
else
format.html { render :new }
format.json { render json: #event.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /events/1
# PATCH/PUT /events/1.json
def update
respond_to do |format|
if #event.update(event_params)
format.html { redirect_to #event, notice: 'Event was successfully updated.' }
format.json { render :show, status: :ok, location: #event }
else
format.html { render :edit }
format.json { render json: #event.errors, status: :unprocessable_entity }
end
end
end
# DELETE /events/1
# DELETE /events/1.json
def destroy
#event.destroy
respond_to do |format|
format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_event
#event = Event.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def event_params
params.require(:event).permit(:title, :description, :start_time, :end_time)
end
end
This is my view:
<p id="notice"><%= notice %></p>
<h1>Listing Events</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Start time</th>
<th>End time</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #events.each do |event| %>
<tr>
<td><%= event.title %></td>
<td><%= event.description %></td>
<td><%= event.start_time %></td>
<td><%= event.end_time %></td>
<td><%= link_to 'Show', event %></td>
<td><%= link_to 'Edit', edit_event_path(event) %></td>
<td><%= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Event', new_event_path %>
<div id="calendar"></div>
<script>
$('#calendar').fullCalendar({
events: '/events.json'});
</script>
I'm following this railscast and got stuck immediately: http://asciicasts.com/episodes/244-gravatar
Whenever I try to edit the index.html file I get this response from the server:
undefined local variable or method `user'.
By the looks of it, it shouldn't seem too difficult. I just need to swap a few lines here and there, but I am having a tough time.
This is what I have in index.html.erb:
<h1>Listing posts</h1>
<% #posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
I would like to change it to this:
<% for user in #users %>
<tr>
<td><%= image_tag avatar_url(user) %></td>
<td><%= user.email %></td>
<td><%= link_to "Show", user %></td>
<td><%= link_to "Edit", edit_user_path(user) %></td>
<td><%= link_to "Destroy", user, :confirm => 'Are you ↵
sure?', :method => :delete %></td>
</tr>
<% end %>
My application helper code:
module ApplicationHelper
def avatar_url(user)
gravatar_id = Digest::MD5::hexdigest(user.email).downcase
"http://gravatar.com/avatar/#{gravatar_id}.png"
end
end
My post controller code:
class PostsController < ApplicationController
# GET /posts
# GET /posts.xml
def index
#posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #posts }
end
end
# GET /posts/1
# GET /posts/1.xml
def show
#post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #post }
end
end
# GET /posts/new
# GET /posts/new.xml
def new
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #post }
end
end
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
# POST /posts
# POST /posts.xml
def create
#post = Post.new(params[:post])
respond_to do |format|
if #post.save
format.html { redirect_to(#post, :notice => 'Post was successfully created.') }
format.xml { render :xml => #post, :status => :created, :location => #post }
else
format.html { render :action => "new" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.xml
def update
#post = Post.find(params[:id])
respond_to do |format|
if #post.update_attributes(params[:post])
format.html { redirect_to(#post, :notice => 'Post was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #post.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.xml
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
end
$ rails g scaffold user email:string
run it in console in the root folder of your application, then make needed changes in app/views/users/index.html.erb
In my case using Devise, I followed the same thing and got stuck as it would give me errors. So I replaced;
module ApplicationHelper
def avatar_url(user)
gravatar_id = Digest::MD5::hexdigest(user.email).downcase
"http://gravatar.com/avatar/#{gravatar_id}.png"
end
end
with;
module ApplicationHelper
def avatar_url(user)
gravatar_id = Digest::MD5::hexdigest(current_user.email).downcase
"http://gravatar.com/avatar/#{gravatar_id}.png"
end
end
I've also noticed that using;
wont work unless you capitalize the "User" looking like this;
I'm very fresh and new to this but after doing that everything works in my app. Hope this helps.
class Question < ApplicationRecord
def gravatar
"http://www.gravatar.com/avatar/#{Digest.MD5.hexdigest(email)}"
end
end
Learnt while going through pluralsight