RoR4: NoMethodError in nested resources' view - ruby-on-rails

I have a parent/children relationship model with nested resources setup in order to create child data under a certain parent from it's show page. However, when I click on the child's new page link from the parent page ("http://localhost:3000/brands/1/restaurants/new"), I get an error saying that "NoMethodError in...". Please help.
config/routes.rb
Rails.application.routes.draw do
resources :brands, shallow: true do
resources :restaurants
end
resources :managers
# Prefix Verb URI Pattern Controller#Action
# brand_restaurants GET /brands/:brand_id/restaurants(.:format) restaurants#index
# POST /brands/:brand_id/restaurants(.:format) restaurants#create
# new_brand_restaurant GET /brands/:brand_id/restaurants/new(.:format) restaurants#new
# edit_restaurant GET /restaurants/:id/edit(.:format) restaurants#edit
# restaurant GET /restaurants/:id(.:format) restaurants#show
# PATCH /restaurants/:id(.:format) restaurants#update
# PUT /restaurants/:id(.:format) restaurants#update
# DELETE /restaurants/:id(.:format) restaurants#destroy
# brands GET /brands(.:format) brands#index
# POST /brands(.:format) brands#create
# new_brand GET /brands/new(.:format) brands#new
# edit_brand GET /brands/:id/edit(.:format) brands#edit
# brand GET /brands/:id(.:format) brands#show
# PATCH /brands/:id(.:format) brands#update
# PUT /brands/:id(.:format) brands#update
# DELETE /brands/:id(.:format) brands#destroy
# managers GET /managers(.:format) managers#index
# POST /managers(.:format) managers#create
# new_manager GET /managers/new(.:format) managers#new
# edit_manager GET /managers/:id/edit(.:format) managers#edit
# manager GET /managers/:id(.:format) managers#show
# PATCH /managers/:id(.:format) managers#update
# PUT /managers/:id(.:format) managers#update
# DELETE /managers/:id(.:format) managers#destroy managers#destroy
end
models/brand.rb
class Brand < ActiveRecord::Base
has_many :restaurants
end
models/restaurants.rb
class Restaurant < ActiveRecord::Base
belongs_to :brand
end
controllers/restaurants_controller.rb
class RestaurantsController < ApplicationController
before_action :set_restaurant, only: [:new, :create, :show, :edit, :update, :destroy]
# GET /restaurants
# GET /restaurants.json
def index
#restaurants = Restaurant.all
end
# GET /restaurants/1
# GET /restaurants/1.json
def show
end
# GET /restaurants/new
def new
# #restaurant = Restaurant.new
#restaurant = Restaurant.new
end
# GET /restaurants/1/edit
def edit
end
# POST /restaurants
# POST /restaurants.json
def create
#restaurant = Restaurant.new(restaurant_params)
respond_to do |format|
if #restaurant.save
format.html { redirect_to brands_path, notice: 'Restaurant was successfully created.' }
format.json { render :show, status: :created, location: #restaurant }
else
format.html { render :new }
format.json { render json: #restaurant.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /restaurants/1
# PATCH/PUT /restaurants/1.json
def update
respond_to do |format|
if #restaurant.update(restaurant_params)
format.html { redirect_to #restaurant, notice: 'Restaurant was successfully updated.' }
format.json { render :show, status: :ok, location: #restaurant }
else
format.html { render :edit }
format.json { render json: #restaurant.errors, status: :unprocessable_entity }
end
end
end
# DELETE /restaurants/1
# DELETE /restaurants/1.json
def destroy
#restaurant.destroy
respond_to do |format|
format.html { redirect_to restaurants_url, notice: 'Restaurant was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_restaurant
#restaurant = Restaurant.find_by(params[:brand_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def restaurant_params
params.require(:restaurant).permit(:name, :brand_id) if params[:restaurant]
end
end
views/brands/show.html
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #brand.name %>
</p>
<p>
<strong>Manager:</strong>
<%= #brand.manager_id %>
</p>
<table>
<% #brand.restaurants.each do |restaurant| %>
<tr>
<td><%= restaurant.name %></td>
<td><%= restaurant.brand_id %></td>
<td><%= link_to 'Show', restaurant %></td>
<td><%= link_to 'Edit', edit_restaurant_path(restaurant) %></td>
<td><%= link_to 'Destroy', restaurant, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br>
**<%= link_to 'Add New Restaurant', new_brand_restaurant_path(#brand) %>**
<%= link_to 'Edit', edit_brand_path(#brand) %> |
<%= link_to 'Back', brands_path %>
What did I do wrong?
Appreciate your help.
Addin Full Error Message:
NoMethodError in Restaurants#new Showing
/Users/kaku/sample/relation/app/views/restaurants/_form.html.erb where
line #1 raised:
undefined method `restaurants_path' for
<#:0x007fd4ec2e46e0> Extracted source (around line #1): 1 2 3 4 5 6
<%= form_for(#restaurant) do |f| %>
<% if #restaurant.errors.any? %>
<%= pluralize(#restaurant.errors.count, "error") %> prohibited this restaurant from being saved:
Trace of template inclusion: app/views/restaurants/new.html.erb
Rails.root: /Users/kaku/sample/relation
Application Trace | Framework Trace | Full Trace
app/views/restaurants/_form.html.erb:1:in
_app_views_restaurants__form_html_erb__1732550980462972679_70276222510780' app/views/restaurants/new.html.erb:3:in
_app_views_restaurants_new_html_erb___209183791776779444_70276222559000'
Request
Parameters:
{"brand_id"=>"2"} Toggle session dump
_csrf_token: "MG3xNH+mscxxPiQqLVkF011BDb5RGlWJPuTN3GiEcGo=" session_id: "e1f92f37d231a058162af62bae976ff5" Toggle env dump
GATEWAY_INTERFACE: "CGI/1.1" HTTP_ACCEPT:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8"
HTTP_ACCEPT_ENCODING: "gzip, deflate, sdch" HTTP_ACCEPT_LANGUAGE:
"ja,en-US;q=0.8,en;q=0.6" REMOTE_ADDR: "::1" REMOTE_HOST: "::1"
SERVER_NAME: "localhost" SERVER_PROTOCOL: "HTTP/1.1" Response
Headers:
None

As you pointed in the code, I believe the error is in this line
<%= link_to 'Add New Restaurant', new_brand_restaurant_path(#brand) %>
You should have #brand in the new method of restaurants_controller
def new
#brand = Brand.find(params[:brand_id])
#restaurant = Restaurant.new
end
And also I noticed that you have defined set_restaurant wrongly. It should be
def set_restaurant
#restaurant = Restaurant.find(params[:id])
end
Update:
undefined method `restaurants_path'
You have nested resources, so this line
<%= form_for(#restaurant) do |f| %>
should be
<%= form_for [#brand, #restaurant] do |f| %>

Related

Rails 6 - 'missing required keys: [:post_id]' when trying to render a 'comment' form directly on my posts#index view

I've been trying to build a facebook/blog type app where comments show directly under new posts, and you can also POST a new comment by rendering a form directly under the corresponding Post. As you'll learn in the rest of my post, I'm fairly new to Rails so any resources that directly help me understand the issue I'm having (even if it's just pointing me to the right parts of the Rails docs) would be super helpful.
As of right now, my homepage renders the post#index action, as well as a a post form so that you can create new posts directly on the index page. All of this works fine until I try to render my comment form. As a note, I'm only focusing on creating a new comment before implementing editing and deleting (not that that should affect anything I wouldn't think).
Unfortunately I keep getting this error -
Moreover, when I split each of these pages into a more traditional, link_to XXXX_path style where every form and action is on its own url page, everything works fine. I think there might be something fundamental that I'm just not understanding. So, before showing my code I'll just give a quick explanation of my understanding of how I expect things to work in my app
On the index.html.erb view:
Render the index page
Show each individual post and any corresponding info I want to display with my each method.
pass the specific instance of Post using <%= render 'comment_form', :post => post %>. This should also give me access to all of the params of said instance of post in my _comment_form.html.erb partial.
On the _comment_form.html.erb partial:
add model: [post, #comment] (as my comment controller is nested in post) so that Rails knows to build a new comment with the associated post instance that was pushed through my index view.
At the end of the day, if I had to guess, my issue is with my controllers - I feel like I've put the correct code in the methods/actions, but my hunch is that there's something conflicting between the #post instance variable and the post instance being iterated over in my each method. I'm really not sure what the problem is and any help would be much appreciated. I hope I didn't over (or under) explain my problem. Thanks for the help in advance!
views/posts/index.html.erb
<p id="notice"><%= notice %></p>
<nav>
</nav>
<h1>Posts</h1>
<table>
<thead>
<tr>
<th>User</th>
<th>Body</th>
<th colspan="3"></th>
</tr>
</thead>
<%= render 'form' %>
<tbody>
<% #posts.each do |post| %>
<tr>
<td><%= link_to post.user.email, user_path(post.user_id) %></td>
<td><%= post.body %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td>
</tr>
<tr>
<%= render 'comment_form', :post => post %>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Post', new_post_path %>
views/posts/_comment_form.html.erb
<% if user_signed_in? %>
<%= form_with(model: [post, #comment], url: post_comments_path, method: "post", local: true) do |form| %>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
<% else %>
Please sign in to comment on the post!
<% end %>
posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
#before_action :authenticate_user!
# GET /posts
# GET /posts.json
def index
#posts = Post.all.order("created_at DESC")
#post = post_exists?
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = current_user.posts.build
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#post = current_user.posts.build(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to index, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Only allow a list of trusted parameters through.
def post_params
params.require(:post).permit(:body)
end
def post_exists?
current_user.posts.build if current_user != nil
end
end
comments_controller.rb
class CommentsController < ApplicationController
def new
#post = Post.find(params[:post_id])
#comment = #post.comments.build
end
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.build(comments_params)
#comment.user_id = current_user.id
respond_to do |format|
if #comment.save
format.html { redirect_to posts_path, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #comment }
else
format.html { render :new }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
def destroy
end
private
def comments_params
params.require(:comment).permit(:body)
end
end
routes.rb
devise_for :users, controllers: {
sessions: 'users/sessions'
}
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
resources :posts do
resources :comments, only: [:new, :create, :destroy]
end
resources :users
root to: 'posts#index'
Models
>user.rb
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
>post.rb
belongs_to :user
has_many :comments, dependent: :destroy
>comment.rb
belongs_to :user
belongs_to :post
The error is saying that it is missing the post_id on line 2 in your comment_form.
You got:
<%= form_with(model: [post, #comment], url: post_comments_path, method: "post", local: true) do |form| %>
The url is set to post_comments_path but it doesn't knows which post so you need to pass the post to it as an argument like so:
<%= form_with(model: [post, #comment], url: post_comments_path(post), method: "post", local: true) do |form| %>
Although this will probably solve the current error, you will hit another error since you also got #comment in the comment_form and in your posts#index you don't declare #comment. You also can't declare #comment because it is depending on the post. You could try to solve that with:
<%= form_with(model: [post, post.comments.new], url: post_comments_path, method: "post", local: true) do |form| %>

uninitialized constant RsvpsController

so i'm getting this error when I press my rsvp button from my ui. Maybe theres something wrong with my route or something wrong with my controller.
I'm not too sure at this point. I'm quite new to rails and I've been stuck on this issue for far too long. When I do make some changes another error pops up. I would truly appreciate some help
class RsvpController < ApplicationController
def create
rsvp = current_user.rsvp.build({post_id: params[:id]})
if rsvp.save
end
end
end
post controller
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
#post = Post.find(params[:id])
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
unless current_user == #post.user
redirect_back fallback_location: root_path, notice: 'User is not owner'
end
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:date, :user_id, :description, :name, :address)
end
load_and_authorize_resource
def create
#rsvp=rsvp.new
end
end
=
routes.rb
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :posts
devise_for :users
root 'home#index'
get 'home/ruby_meetup'
resources :posts do
post 'rsvp', to: 'rsvps#create', on: :member
end
Also I want the number of users who rsvped/registered to show up but I'm getting random numbers and letters showing up on my ui. Is there something not right with my show page?
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Date:</strong>
<%= #post.date %>
</p>
<p>
<strong>Name:</strong>
<%= #post.name %>
</p>
<p>
<strong>User_id:</strong>
<%= #post.user_id %>
</p>
<p><strong>Address:</strong> <%= #post.address %></p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<p>
<strong>registered:</strong>
<%=#post.user %>
</p>
<% if current_user == #post.user %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<div class="rsvp"><%= button_to "Rsvp now", rsvp_post_path(#post), class: "btn btn-primary" %></div>
<div class="map"><%= image_tag "http://maps.googleapis.com/maps/api/staticmap?center=#{#post.latitude},#{#post.longitude}&markers=#{#post.latitude},#{#post.longitude}&zoom=12&size=450x400&key=AIzaSyCKzKMEhSNgwSXf7WV71pHWgzdpMkPn8W4",
class: 'img-fluid img-rounded', alt: "#{#post} on the map"%>
</div>
controller class name should be RsvpsController currently its RsvpController, note that s is missing after Rsvp.
Change your route to this:
post 'rsvp', to: 'rsvp#create', on: :member

NoMethodError in Jobs#show

I've been stuck on this all day. When I try to show the details of a job, create a job, or edit a current job in my ruby on rails project I get a NoMethodError in Jobs#show on the second last line explaining there is a problem with the link to the edit page.
Jobs/show.html
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #job.name %>
</p>
<p>
<strong>Employer:</strong>
<%= #job.employer %>
</p>
<p>
<strong>Sector:</strong>
<%= #job.sector_id %>
</p>
<p>
<strong>Experience req:</strong>
<%= #job.experience_req %>
</p>
<p>
<strong>Job info:</strong>
<%= #job.job_info %>
</p>
<h2>Star comment: </h2>
<%=form_for([#job, Request.new]) do |f| %>
</h3></br>
<%= f.text_area:content, :rows => 4, :cols=> 40%>
<div class = "actions">
<%=f.submit "Make a request for the job"%>
</div>
<% end %>
<%if #job.requests.empty? %>
<h3> You are the first to Request</h3>
<% else %>
<h2> Who else had made a request for this job:</h2>
<% #job.requests.reverse.each do |request| %>
<p><%= request.content %>
Posted <%=time_ago_in_words(request.created_at)%> ago by
<%=request.candidate.can_name%></p>
<% end %>
<% end %>
<%= link_to 'Edit', edit_jobs_path(#job) %> | **This line highlights an error**
<%= link_to 'Back', jobs_path %>
Jobs/edit.html
<h1>Editing job</h1>
<%= render 'form' %>
<%= link_to 'Show', #job %> |
<%= link_to 'Back', jobs_path %>
Routes
Rails.application.routes.draw do
# get 'sessions/new'
# get 'sessions/create'
#get 'sessions/destroy'
controller :sessions do
get 'login' =>:new
post 'login' =>:create
get 'logout' =>:destroy
delete 'logout' =>:destroy
end
#get 'pages/home'
#get 'pages/about'
resources :candidates
resources :requests
resources :employers
resources :jobs
resources :sectors
# The priority is based upon order of creation: first created -> highest
# priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'pages#home'
#root :to=>'pages#home
#'welcome#index'
resources :jobs do
resources :requests
end
end
Jobs_Controller
class JobsController < ApplicationController
before_action :set_job, only: [:show, :edit, :update, :destroy]
# GET /jobs
# GET /jobs.json
def index
#jobs = Job.all
end
# GET /jobs/1
# GET /jobs/1.json
def show
end
# GET /jobs/new
def new
#job = Job.new
end
# GET /jobs/1/edit
def edit
end
# POST /jobs
# POST /jobs.json
def create
#job = Job.new(job_params)
respond_to do |format|
if #job.save
format.html { redirect_to #job, notice: 'Job was successfully created.'
}
format.json { render :show, status: :created, location: #job }
else
format.html { render :new }
format.json { render json: #job.errors, status: :unprocessable_entity
}
end
end
end
# PATCH/PUT /jobs/1
# PATCH/PUT /jobs/1.json
def update
respond_to do |format|
if #job.update(job_params)
format.html { redirect_to #job, notice: 'Job was successfully updated.'
}
format.json { render :show, status: :ok, location: #job }
else
format.html { render :edit }
format.json { render json: #job.errors, status: :unprocessable_entity }
end
end
end
# DELETE /jobs/1
# DELETE /jobs/1.json
def destroy
#job.destroy
respond_to do |format|
format.html { redirect_to jobs_url, notice: 'Job was successfully
destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_job
#job = Job.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white
#list through.
def job_params
params.require(:job).permit(:name, :employer, :sector_id,
:experience_req,:job_info)
end
end
Update
Ursus informed me I had to change <%= link_to 'Show', #job %> to<%= link_to 'Show', job_path(#job) %> in Jobs/edit.html. I did this, however when I try to create a new job or edit a current job I still get the same error but the new job is still created?
This
<%= link_to 'Show', #job %>
should be
<%= link_to 'Show', job_path(#job) %>

update field from a link_to in rails 4

I just need a link_to link that automatically updates a value.
I have done the following:
1) Added the field i want to update into the post_controller permit list:
#post_controller.rb
def post_params
params.require(:post).permit(:title, :description, :file, :user_id, :category_id, :revisor_id, :visible)
end
2) Added the link_to link in the view:
<td><%= link_to 'Publish', post_path(post, visible: true), method: :put %></td>
any idea what im doing wrong?
#routes.rb
resources :posts do
get 'revisions', on: :collection
end
the error i'm getting is:
param is missing or the value is empty: post
Here is the controller:
#posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
def revisions
#posts = Post.where(revisor_id: current_user.id)
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
#post.visible = true
#post.user_id = current_user.id
#petition = Petition.new(user_id: current_user.id, category_id: #post.category_id, status: "New")
#petition.save
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :description, :file, :user_id, :category_id, :revisor_id, :visible)
end
end
Here is the view:
#revisions.html.erb
<h1>Pending for revision</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>User</th>
<th>Category</th>
<th>Revisor</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #posts.pending_posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.description %></td>
<td><%= post.user_id %></td>
<td><%= post.category_id %></td>
<% if !post.revisor_id %>
<td> No asignado </td>
<% else %>
<td><%= post.revisor_id %></td>
<% end %>
<td><%= link_to 'Publish', post_path(post, visible: true), method: :put %></td>
<!--<td><%#= link_to 'Edit', edit_petition_path(petition) %></td>-->
<!--<td><%#= link_to 'Destroy', petition, method: :delete, data: { confirm: 'Are you sure?' } %></td>-->
</tr>
<% end %>
</tbody>
</table>
Here is the model:
#post.rb
class Post < ActiveRecord::Base
belongs_to :category
belongs_to :user
has_one :petition
scope :my_revisions, ->(user){ where("posts.user_id = ?", user.id)}
scope :visible_posts, -> {where(visible: true)}
scope :pending_posts, -> {where(visible: false)}
end
SERVER LOG:
Started PUT "/posts/12?visible=true" for 127.0.0.1 at 2014-07-16 10:07:31 +0100
Processing by PostsController#update as HTML
Parameters: {"authenticity_token"=>"authenticitytoken=", "visible"=>"true", "id"=>"12"}
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 12]]
Completed 400 Bad Request in 2ms
ActionController::ParameterMissing (param is missing or the value is empty: post):
app/controllers/posts_controller.rb:79:in `post_params'
app/controllers/posts_controller.rb:51:in `block in update'
app/controllers/posts_controller.rb:50:in `update'
Rendered /home/kbs23/.rvm/gems/ruby-2.1.0#global/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.6ms)
Rendered /home/kbs23/.rvm/gems/ruby-2.1.0#global/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.5ms)
Rendered /home/kbs23/.rvm/gems/ruby-2.1.0#global/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
Rendered /home/kbs23/.rvm/gems/ruby-2.1.0#global/gems/actionpack-4.1.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (13.7ms)
Here are my routes:
#rake routes
revisions_posts GET /posts/revisions(.:format) posts#revisions
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
I finally got to fix this by just changing the link from:
<td><%= link_to 'Publish', post_path(post, visible: true), method: :put %></td>
to this:
<td><%= link_to 'Publish', post_path(post, post: {visible: :true}), method: :put %></td>

Rails 4.0 ActiveRecord::RecordNotFound

I am trying to learn Rails and am making my first app and am running into this error:
ActiveRecord::RecordNotFound in PartsController#show
Couldn't find Part with id=new_ic
with the highlighted source:
def set_part
#part = Part.find(params[:id])
end
I am brand new to rails and i can't figure out what is wrong and I can't find any help online either. The app is a part management system for electronic components. The form gets filled out and the data is saved to the database for future reference/updating. Could someone please help?
Source code time:
parts/_ic_form.html.erb
<h1>Add An IC</h1>
<%= simple_form_for #parts do |f| %>
<%= f.input :component_type, :as => :hidden, :input_html => { :value => "IC"} %>
<%= f.input :ic_model, label: 'IC Model' %>
<%= f.input :ic_manufacturer, label: 'IC Manufacturer' %>
<%= f.input :ic_pinCount, label: 'IC Pin-Count' %>
<%= f.input :ic_mountType, collection: ["Through Hole", "Surface Mount"], label: 'IC Mount Type' %>
<%= f.input :ic_package, label: 'IC Package' %>
<%= f.input :ic_quantityOnHand, label: 'Quantity On Hand' %>
<%= f.input :ic_quantityOnOrder, label: 'Quantity On Order' %>
<%= f.button :submit %>
<% end %>
parts/new_ic.html.erb
<%= render 'ic_form' %>
parts/new.html.erb
<h1>New part</h1>
<%= link_to 'IC', 'new_ic' %>
<%= link_to 'Back', parts_path %>
parts_controller.rb
class PartsController < ApplicationController
before_action :set_part, only: [:show, :edit, :update, :destroy]
before_filter :initialize_parts
def initialize_parts
#parts = Part.new
end
# GET /parts
# GET /parts.json
def index
#parts = Part.all
end
# GET /parts/1
# GET /parts/1.json
def show
end
# GET /parts/new
def new
#part = Part.new
end
# GET /parts/1/edit
def edit
end
# POST /parts
# POST /parts.json
def create
#part = Part.new(part_params)
respond_to do |format|
if #part.save
format.html { redirect_to #part, notice: 'Part was successfully created.' }
format.json { render action: 'show', status: :created, location: #part }
else
format.html { render action: 'new' }
format.json { render json: #part.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /parts/1
# PATCH/PUT /parts/1.json
def update
respond_to do |format|
if #part.update(part_params)
format.html { redirect_to #part, notice: 'Part was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #part.errors, status: :unprocessable_entity }
end
end
end
# DELETE /parts/1
# DELETE /parts/1.json
def destroy
#part.destroy
respond_to do |format|
format.html { redirect_to parts_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_part
#part = Part.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def part_params
params[:part]
end
end
routes.rb Pretty sure i screwed this one up too
Pms::Application.routes.draw do
resources :parts
resources :parts
root to: "parts#new_ic"
end
rake routes Output:
Prefix Verb URI Pattern Controller#Action
parts GET /parts(.:format) parts#index
POST /parts(.:format) parts#create
new_part GET /parts/new(.:format) parts#new
edit_part GET /parts/:id/edit(.:format) parts#edit
part GET /parts/:id(.:format) parts#show
PATCH /parts/:id(.:format) parts#update
PUT /parts/:id(.:format) parts#update
DELETE /parts/:id(.:format) parts#destroy
GET /parts(.:format) parts#index
POST /parts(.:format) parts#create
GET /parts/new(.:format) parts#new
GET /parts/:id/edit(.:format) parts#edit
GET /parts/:id(.:format) parts#show
PATCH /parts/:id(.:format) parts#update
PUT /parts/:id(.:format) parts#update
DELETE /parts/:id(.:format) parts#destroy
root GET / parts#new_ic
One problem is in this line:
<%= link_to 'IC', 'new_ic' %>
link_to should look like this:
link_to "Profile", profile_path(#profile)
#Profile is the name
#profile_path(#profile) is the link
Try this instead:
#parts/new.html.erb
<%= link_to 'IC', root_path %>
in your routes, root GET / parts#new_ic is linking to your new_ic action. I'd disagree with the way you access it (via root) - but it will work if you want to access the new_ic action. Why is this your root route, though?

Resources