ActionController::UrlGenerationError in Interfaces#index - ruby-on-rails

I am trying to delete my interface,and it said
No route matches {:action=>"show", :controller=>"interfaces", :project_id=>#}, missing required keys: [:id].
Because I do not know which arguments should be put into <%=link_to '删除', project_interface_path()%>
I have tried many different arguments into a path.
interface_controller.rb
def destroy
#interface = #project.interfaces.find(params[:project_id])
redirect_to project_interfaces_path if #interface.destroy
end
def index
#interfaces = Interface.all
#project = Project.find(params[:project_id])
end
def new
#project = Project.find(params[:project_id])
#interface = Interface.new
end
def create
#project = Project.find(params[:project_id])
#interface = #project.interfaces.new(interface_params)
if #interface.save
redirect_to project_interfaces_path
else
render :new
end
end
interface/index.html.erb
<% #interfaces.each do |interface| %>
<tr>
<th scope="row">1</th>
<td><%=interface.name %></td>
<td><%=interface.desp %></td>
<td><%=interface.method_type_id %></td>
<td><%=interface.request_url %></td>
<td><%=interface.request_eg %></td>
<td><%=interface.response_eg %></td>
</tr>
<td><%= link_to '删除', project_interface_path(interface),method: :delete, data:{confirm:'确定要删除吗?'}, class: 'badge badge-dark' %></td>
This is my route file:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root "method_types#index"
resources :method_types
resources :field_types
resources :projects do
resources :interfaces
end
end

Try this:
<%= link_to '删除', project_interface_path(#project.id, interface.id),method: :delete, data:{confirm:'确定要删除吗?'}, class: 'badge badge-dark' %></td>

Related

Rails 4 uninitialized constant Admin::Category

I have generated Admin namespaced Controllers for all my default models as follows:
rails g scaffold_controller admin/categories name:string slug:string description:string icon_xlarge:string icon_large:string icon_medium:string icon_small:string status:integer
This generated the following files:
Harshas-MacBook-Pro:nomad harshamv$ rails g scaffold_controller admin/categories name:string slug:string description:string icon_xlarge:string icon_large:string icon_medium:string icon_small:string status:integer
Plural version of the model detected, using singularized version. Override with --force-plural.
create app/controllers/admin/categories_controller.rb
invoke erb
create app/views/admin/categories
create app/views/admin/categories/index.html.erb
create app/views/admin/categories/edit.html.erb
create app/views/admin/categories/show.html.erb
create app/views/admin/categories/new.html.erb
create app/views/admin/categories/_form.html.erb
invoke test_unit
create test/controllers/admin/categories_controller_test.rb
app/model/category.rb
class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
has_and_belongs_to_many :venues
end
app/controller/admin/categories_controller.rb
class Admin::CategoriesController < ApplicationController
before_action :set_admin_category, only: [:show, :edit, :update, :destroy]
# GET /admin/categories
def index
#admin_categories = Admin::Category.all
end
# GET /admin/categories/1
def show
end
# GET /admin/categories/new
def new
#admin_category = Admin::Category.new
end
# GET /admin/categories/1/edit
def edit
end
# POST /admin/categories
def create
#admin_category = Admin::Category.new(admin_category_params)
if #admin_category.save
redirect_to #admin_category, notice: 'Category was successfully created.'
else
render :new
end
end
# PATCH/PUT /admin/categories/1
def update
if #admin_category.update(admin_category_params)
redirect_to #admin_category, notice: 'Category was successfully updated.'
else
render :edit
end
end
# DELETE /admin/categories/1
def destroy
#admin_category.destroy
redirect_to admin_categories_url, notice: 'Category was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_admin_category
#admin_category = Admin::Category.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def admin_category_params
params.require(:admin_category).permit(:name, :slug, :description, :icon_xlarge, :icon_large, :icon_medium, :icon_small, :status)
end
end
app/view/admin/categories/index.html.erb
<h1>Listing admin_categories</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Slug</th>
<th>Description</th>
<th>Icon xlarge</th>
<th>Icon large</th>
<th>Icon medium</th>
<th>Icon small</th>
<th>Status</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #admin_categories.each do |admin_category| %>
<tr>
<td><%= admin_category.name %></td>
<td><%= admin_category.slug %></td>
<td><%= admin_category.description %></td>
<td><%= admin_category.icon_xlarge %></td>
<td><%= admin_category.icon_large %></td>
<td><%= admin_category.icon_medium %></td>
<td><%= admin_category.icon_small %></td>
<td><%= admin_category.status %></td>
<td><%= link_to 'Show', admin_category %></td>
<td><%= link_to 'Edit', edit_admin_category_path(admin_category) %></td>
<td><%= link_to 'Destroy', admin_category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Category', new_admin_category_path %>
My Attempts
I edited the Controller as below
# GET /admin/categories
def index
#admin_categories = Category.all
end
# GET /admin/categories/1
def show
end
# GET /admin/categories/new
def new
#admin_category = Category.new
end
# GET /admin/categories/1/edit
def edit
end
# POST /admin/categories
def create
#admin_category = Category.new(admin_category_params)
if #admin_category.save
redirect_to #admin_category, notice: 'Category was successfully created.'
else
render :new
end
end
When I go to localhost/admin/categories and click "NEW category", I get the following error now:
My routes file:
Rails.application.routes.draw do
# Admin Routing
namespace :admin do
resources :categories, :cities, :countries, :lists, :oauths, :regions, :tags, :users, :user_groups, :venues, :venue_photos, :venue_reviews
end
end
You have resources :categories defined under the namespace :admin in your routes.rb,so this line in your views/admins/categories/_form.html.erb
<%= form_for(#admin_category) do |f| %>
should be
<%= form_for([:admin, #admin_category]) do |f| %>
For more info,refer this API
Update
The second error is because of this line
params.require(:admin_category).permit(:name, :slug, :description, :icon_xlarge, :icon_large, :icon_medium, :icon_small, :status)
It should be
params.require(:category).permit(:name, :slug, :description, :icon_xlarge, :icon_large, :icon_medium, :icon_small, :status)
To access models outside of the namespace you need to call ::Category.new instead of Admin::Category.new
As your error indicates, this is an issue with how you are calling your form. Your form needs to reference the admin namespace, like this:
<%= form_for [:admin, #category] do |f| %>
However, there are a number of things different about the way the scaffold built your docs from how I would recommend.
I would also suggest simplifying the code in the scaffolded controller to reference simple #category and #categories, rather than #admin_category and #admin_categories.
Also, the model should not be in the admin namespace, so Admin::Category.new should be Category.new. The rest of the model calls should be adjusted accordingly as well.

Ruby getting started guide: uninitialized constant PostsController::Posts

I've done the ruby getting started guide a few times over and I always end up with the same result.
Guids
NameError in PostsController#index
uninitialized constant PostsController::Posts
Extracted source (around line #21):
19
20
21
22
23
24
def index
#posts = Posts.all
end
def edit
Rails.root: C:/RailsTesting/blog
Application Trace | Framework Trace | Full Trace
app/controllers/posts_controller.rb:21:in `index'
Request
Out of frustration/desperation, I copied all of the files from the actual code supplied by the tutorial with no avail, please help.
Here's my index
<h1>Hello, Rails!</h1>
<%= link_to "My Blog", controller: "posts" %>
<%= link_to 'New post', new_post_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th></th>
<th></th>
</tr>
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post_path(post),
method: :delete, data: { confirm: 'Are you sure?' } %>
</tr>
...
Here is my posts_controller
class PostsController < ApplicationController
def new
#post = Post.new
end
def create
#post = Post.new(params[:post].permit(:title, :text))
if #post.save
redirect_to #post
else
render 'new'
end
end
def show
#post = Post.find(params[:id])
end
def index
#posts = Posts.all
end
def edit
#post = Post.find(params[:id])
end
def update
#post = Post.find(params[:id])
if #post.update(params[:post].permit(:title, :text))
redirect_to #post
else
render 'edit'
end
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
Your index method has the Post class written in plural. Change it to Post.all
Modify your index action as:
def index
#posts = Post.all
end
Here Post is the model from where you are get all posts.

Rails 4: No route matches - issue with relationships missing id key

I am hoping someone can help me. I am getting the following issue:
No route matches {:action=>"show", :controller=>"stocks", :stockpile_id=>#<Stock id: 17, stockpile_id: 3, code: "rttrtrt", name: "", description: "", quantity: nil, cost_pence: nil, information: "", created_at: "2013-08-18 19:52:46", updated_at: "2013-08-18 19:52:46">, :id=>nil, :format=>nil} missing required keys: [:id]
When visiting the following URL: /admin/stockpiles/3/stocks/
My routes look like:
scope '/admin' do
root :to => 'admin#index', :as => 'admin'
resources :stockpiles,:companies
scope :path => 'stockpiles/:stockpile_id' do
resources :stocks
end
end
The data contained in the error message:
id | stockpile_id | code | name | description | quantity | cost_pence | information | created_at | updated_at
17 | 3 | rttrtrt | | | | | | 2013-08-18 19:52:46.856864 | 2013-08-18 19:52:46.856864
My Stock Model:
class Stock < ActiveRecord::Base
belongs_to :stockpile
end
Nothing interesting in the Stockpile model, just that it has many stocks..
Here is my controller:
class StocksController < ApplicationController
before_action :set_stock, only: [:show, :edit, :update, :destroy]
def index
#stockpile = Stockpile.find(params[:stockpile_id])
#stocks = #stockpile.stocks.all
end
def show
end
def new
#stockpile = Stockpile.find(params[:stockpile_id])
#stock = #stockpile.stocks.new
end
def edit
end
def create
#stockpile = Stockpile.find(params[:stockpile_id])
#stock = #stockpile.stocks.new(stock_params)
if #stock.save
redirect_to #stock, notice: 'Stock was successfully created.'
else
render action: 'new'
end
end
def update
if #stock.update(stock_params)
redirect_to #stock, notice: 'Stock was successfully updated.'
else
render action: 'edit'
end
end
def destroy
#stock.destroy
redirect_to stocks_url, notice: 'Stock was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_stock
#stockpile = Stockpile.find(params[:stockpile_id])
#stock = #stockpile.stocks.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def stock_params
params.require(:stock).permit(:code, :name, :description, :quantity, :cost_pence, :information)
end
end
And here is the view in question:
<div class="page-header">
<%= link_to new_stock_path(params[:stockpile_id]), :class => 'btn btn-primary' do %>
<i class="icon-plus icon-white"></i>
New Stock
<% end %>
<h1>Listing stocks</h1>
</div>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Stockpile</th>
<th>Code</th>
<th>Name</th>
<th>Description</th>
<th>Quantity</th>
<th>Cost pence</th>
<th>Information</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #stocks.each do |stock| %>
<tr>
<td><%= stock.stockpile %></td>
<td><%= stock.code %></td>
<td><%= stock.name %></td>
<td><%= stock.description %></td>
<td><%= stock.quantity %></td>
<td><%= stock.cost_pence %></td>
<td><%= stock.information %></td>
<td><%= link_to 'Show', stock %></td>
<td><%= link_to 'Edit', edit_stock_path(stock) %></td>
<td><%= link_to 'Destroy', stock, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</tbody>
</table>
Help would be much appreciated - looks like something weird is going on because stockpile_id seems to be set to stock, and there seems to be no stock params or anything - so we get the missing id error.
Thanks.
It would appear that you need to pass in the stockpile as well as the stock..
<td><%= link_to 'Show', [#stockpile, stock] %></td>
Should do that.
As opposed to using the scope you can probably just use a nested resource in your routes
resources :stockpiles do
resources :stocks
end
and then you can DRY up your stock controller
class StocksController < ApplicationController
before_action :set_stockpile
.....
def set_stockpile
#stockpile = Stockpile.find params[:stockpile_id]
end

rails unit/test get ActionView::Template::Error: undefined method `name' for nil:NilClass

there is a easy dependencies in my exercise_project
patients belongs_to location
location has_many patients
my patients_controller.rb file
class PatientsController < ApplicationController
def location
#location = Location.find(params[:id])
place = #location.id
#patients = Patient.where(:location_id => place)
end
def index
#patients = Patient.paginate(:page => params[:page], :per_page => 20).order('id DESC')
respond_to do |format|
format.html
format.json { render json: #patients }
end
end
others are same with which generate by rails default
my patients#index file
<table>
<% #patients.each do |patient| %>
<tr>
<td><%= patient.name %> </td>
<td><%= patient.birthday %> </td>
<td><%= patient.gender %></td>
<td><%= medical_record(patient.id) %></td>
<td><%= patient.status %></td>
<td><%= link_to(patient.location.name, location_path(patient.location)) %> </td>
<td><%= patient.viewed_count %></td>
</tr>
<td><%= link_to 'Show', patient_path(patient) %></td>
<td><%= link_to 'Edit', edit_patient_path(patient) %></td>
<td><%= link_to 'Destroy', patient_path(patient), :method => :delete, :confirm => 'Are YOU SURE?' %></td>
<% end %>
</table>
<%= link_to "create patient", new_patient_path %><hr>
<%= link_to "Locations", locations_path %>
and I write a unit/test for the controller
require 'test_helper'
class PatientsControllerTest < ActionController::TestCase
def setup
#patient = patients(:one)
#location = locations(:one)
#patient.location_id = #location.id
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:patients)
end
end
When I run the test, it returns
1) Error:
test_should_get_index(PatientsControllerTest):
ActionView::Template::Error: undefined method `name' for nil:NilClass
I do know it must be the location method in the controller that makes it, but I can't find a way to solve this.
How could write the right test for this controller?
---------------------updated------------------------------------
I change it to this
require 'test_helper'
class PatientsControllerTest < ActionController::TestCase
def setup
#patient = patients(:one)
#location = locations(:one)
#patient.location_id = #location.id
end
test "should get index" do
get :location
get :index
assert_response :success
assert_not_nil assigns(:patients)
end
end
and get ActionController::RoutingError: No route matches {:controller=>"patients", :action=>"location"}
my routes.rb
Patients::Application.routes.draw do
resources :locations
resources :patients
root to: "locations#index"
match "patients/location/:id", :to => "patients#location", :as => :location_patients
end
get :location
...(and more text because stackoverflow requires 30 character answers.)

Link_to Routing Issue With Nested Resources

I have two models Jobs and Questions. A job has many questions and questions belong to a job.
I've set up the resources in the model, as well as the routes. I am having an issue trying to link_to the Show method of the questions controller on the questions#index page. My rake routes say that the path should be job_question_path with the two necessary :id's being :job_id and :id , so I tried:
<td><%= link_to 'Show', job_question_path(#job, question) %></td>
and got the error:
No route matches {:action=>"show", :controller=>"questions", :job_id=>nil, :id=>#<Question id: 1, job_id: 1, question1: "sfsdfssfs", question2: "sfsdfs", question3: "sfsdf", question4: "sfsdfsf", question5: "sfsfsfs", created_at: "2011-06-21 03:25:12", updated_at: "2011-06-21 03:25:12">}
I've tried multiple other combos and nothing is seeming to work, I keep getting:
No route matches {:action=>"show", :controller=>"questions", :job_id=>nil }
or some combination of that.
The part I don't get is that I can put in the url /jobs/1/questions/1 and it takes me to the show page, so I am assuming that my questions#show methods are ok. See below for the rest of my code.
Questions#index view
<% #questions.each do |question| %>
<tr>
<td><%= question.question1 %></td>
<td><%= question.question2 %></td>
<td><%= question.question3 %></td>
<td><%= question.question4 %></td>
<td><%= question.question5 %></td>
<td><%= link_to 'Show', job_question_path(#job, question) %></td>
</tr>
<% end %>
Questions Controller
def index
#questions = Question.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #questions }
end
end
def show
#job = Job.find(params[:job_id])
#question = #job.questions.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #question }
end
end
Models
class Job < ActiveRecord::Base
has_many :questions
class Question < ActiveRecord::Base
belongs_to :job
Routes.rb
root :to => "pages#home"
resources :jobs do
resources :questions
end
get "pages/home"
get "pages/about"
get "pages/contact"
See this https://gist.github.com/1032734 for my rake routes.
Thanks for any help in advance, i've been at this for a while now and just can't figure out the solution. Please let me know if you need any more info.
may be so?
Questions#index view
<% #questions.each do |question| %>
<tr>
<td><%= question.question1 %></td>
<td><%= question.question2 %></td>
<td><%= question.question3 %></td>
<td><%= question.question4 %></td>
<td><%= question.question5 %></td>
<%= link_to 'Show', job_question_path(question.job_id, question.id) %>
</tr>
It have to work. Or haven't you 'job_id' field in Questions table ?

Resources