I don't know what is it happened with my app. I have some controllers that are working perfectly but this one not.
my sylabus_controller.rb
# encoding: utf-8
module Admin
class SylabusController < BaseController
def destroy
#sylabu = #topic.sylabus.find(params[:sylabus])
#sylabu.destroy
redirect_to admin_course_topic_sylabu_path(#course, #topic), notice: 'Sylabus deleted'
end
my /views/admin/sylabus/index.html.rb
<% #sylabu.each do |syla| %>
<tr>
<td><%= syla.mupet_code %></td>
<td><%= syla.name %></td>
<td style="width:155px">
<%= link_to '<i class="icon-pencil"></i>'.html_safe, edit_admin_course_topic_sylabus_path(#course,
#topic,
syla),
class: 'btn' %>
<%= link_to '<i class="icon-trash icon-white"></i>'.html_safe, [:admin, #course, #topic, syla], class: 'btn btn-danger', method: :delete, data: { confirm: 'Are you sure?' } %>
<%= link_to '<i class="icon-eye-open"></i>'.html_safe, [:admin, #course, #topic, syla],
class: 'btn' %>
</td>
</tr>
<% end %>
The target web is in the button delete is localhost:3000/admin/courses/1/topics/2/sylabus.8 and with the following error message
Routing Error
No route matches [DELETE] "/admin/courses/1/topics/2/sylabus.8"
Try running rake routes for more information on available routes.
If I execute rake routes from my console
POST /admin/courses/:course_id/topics/:topic_id/sylabus(.:format) admin/sylabus#create
new_admin_course_topic_sylabu GET /admin/courses/:course_id/topics/:topic_id/sylabus/new(.:format) admin/sylabus#new
edit_admin_course_topic_sylabu GET /admin/courses/:course_id/topics/:topic_id/sylabus/:id/edit(.:format) admin/sylabus#edit
admin_course_topic_sylabu GET /admin/courses/:course_id/topics/:topic_id/sylabus/:id(.:format) admin/sylabus#show
PUT /admin/courses/:course_id/topics/:topic_id/sylabus/:id(.:format) admin/sylabus#update
DELETE /admin/courses/:course_id/topics/:topic_id/sylabus/:id(.:format) admin/sylabus#destroy
Sincerely I don't know from the error is coming because it's a copy and paste from other controllers that are working perfectly.
Well thank you very much for your answers.
Have a great day
By the .8 suffix it looks like it's using the index path instead of the delete path. Try being explicit
<%= link_to 'blah'.html_safe, admin_course_topic_sylabus_path(:admin, #course, #topic, syla), method: :delete %>
Related
I'm trying to pass parameters using link_to with ruby on rails, but it says the id parameter I'm sending is null.
code from where I'm sending the id.
<% #conference.papers.each do |paper| %>
<tr>
<td><%= paper.title %></td>
<td><%= paper.author %></td>
<td><%= link_to "Download Paper", paper.attachment_url %></td>
<td><%= link_to 'Reviews', paper %></td>
<% if (paper.accepted) %>
<td><%= "Accepted" %></td>
<% else %>
<td><%= "Not accepted" %></td>
<% end %>
<% if (#state1 && paper.accepted == false) %>
<td><%= button_to "Accept", accept_paper_path(id: paper.id), class: "btn btn-danger", data: { confirm: "Are you sure that you wish to accept #{paper.title}?"} %></td>
<% end %>
<% if (#state2) %>
<% session["a"] = paper.id %>
<td><%= link_to "Review paper", new_review_path(id: paper) %></td>
<% end %>
</tr>
<% end %>
code for the review controller
def new
#paper = Paper.find_by_id(params[:id])
#review = Review.new()
end
You missed .id in
link_to "Review paper", new_review_path(id: paper.id)
But it is not a good solution. If your Paper model has_many :reviews, it would be better to nest reviews routes in paper's ones. Like this:
# config/routes.rb
resources :papers do
resources :reviews
end
And so, your link_to will look like:
link_to "Review paper", new_paper_review_path(paper)
which will generate
/papers/:paper_id/reviews/new
You can learn more about Rails routing here.
Lets start by setting up the routes properly:
resouces :papers do
member do
patch :accept
end
end
This will let you accept a review by PATCH /papers/:id. To create the button use:
<%= button_to accept_paper_path(paper), method: :patch %>
Note that this should use the PATCH or PUT http method - not GET since it is a non-idempotent action.
Note that you can just pass the model instead of doing accept_paper_path(id: model) or accept_paper_path(id: model.id).
For reviews you will want to create what is called a nested resource:
resouces :papers do
member do
patch :accept
end
resources :reviews, only: [:new, :create]
end
This gives you the route /papers/:paper_id/reviews/new.
<%= link_to "Review paper", new_paper_review_path(paper) %>
To set the form to create a new review to the use correct path use an array containing the parent and child:
<%= form_for([#paper, #review]) %>
this code:
<%= link_to 'Show', home %></td>
<%= link_to 'New Post', new_home_path %>
that code above make by default scaffold,
if i add code like this:
<%= link_to 'About', about %></td>
->:
<%= link_to 'Show', home %></td>
<%= link_to 'About', about %></td>
<%= link_to 'New Post', new_home_path %>
then run/refresh show error,why error? i know that error is add code <%= link_to 'About', about %></td> but I see in homesController nothing see home and new_home_path? and in routers.rb same.
To get about page, you need to create route, controller and view page.
rails g controller static about
This url will work:
<%= link_to 'About', static_about_path %></td>
if you want just: about_path instead of static_about_path
then in config/routes.rb file
change -> get 'static/about' to get 'about' => 'static#about'
At this time I want to put a button in the user's list "Disconnect" to finish the session when I need it
I'm using sorcery authentication
I added a new route in "routes.rb"
get "killsession" => "users#killsession", :as => "killsession"
I also Have a method in the "users_controller.rb"
def killsession
session[:user_id] = nil
redirect_to root_url
end
And in the view in the file "index.html.erb" I have it
<% #users.each do |user| %>
<tr>
<td><%= image_tag "#{user.definir_status_icono}" %></td>
<td><%= user.id %></td>
<td><%= user.username.capitalize %></td>
<td><%= user.name %></td>
<td><%= user.last_login_at.strftime("%l:%M %p, %B %d, %Y") rescue nil %></td>
<td>
<%= user.last_logout_at.strftime("%l:%M %p, %B %d, %Y") rescue nil %>
</td>
<td>
<%= link_to "Ver Mas", edit_user_path(user),
:class => 'btn btn-mini btn-primary' %>
<%= link_to 'Eliminar', user_path(user),
:confirm => "Esta seguro ?",
:method => :delete,
:remote => true,
:class => 'btn btn-mini btn-danger' %>
<% if user.online == 1 %>
<%= link_to "Disconnect", killsession_path(user),
:class => 'btn btn-mini btn-warning' %>
<% else %>
<%= link_to "Disconnect", '',
:class => 'btn btn-mini btn-warning',
:disabled => true %>
<% end %>
</td>
</tr>
<% end %>
I don't know how to do that button because when I clicked the button just finish the current session, and doesn't finish the especific user's session
Thanks for your help
The session hash is local to, well, your session. Read about it here. So when you modify it, it affects no one else. This approach won't work.
Generally, you don't have direct access to other users' session data. If you use cookie store (default!), it is stored locally in their browser. If you used for example ActiveRecordStore, you could possibly tamper with database table directly. But I would strongly discourage it - for security reasons as well as possibility to change session store easily.
So now, you want to sign out another user - any authentication library should allow it. If you use devise, it's as simple as sign_out user. If you wrote your own authentication module, you should know how to destroy a session (maybe resetting user's session token in users table?)
Why not just follow this ASCIIcast and log the user out? This seems simpler than actually trying to destroy a user's session manually.
Maybe because your routes are wrong ? Destroy a session is a DELETE not a GET and you set a params in your index.html.erb using killsession_path(user)
# The :as is useless
delete "/killsession/:id_of_selected_user" => "users#killsession"
and your button have to be like that :
<%= button_to 'Logout', killsession_path(:id_of_selected_user => user.id), :method => :delete %>
I'm creating a simple inventory app, there is a view that lists 'items'. It has tables with these rows:
<tr>
<td><%= item.title %></td>
<td><%= item.desc %></td>
<td><%= item.value %></td>
<td><%= item.room.name %></td>
<td><%= item.user.username %></td>
<td>
<%= link_to 'View', item %>
<%= link_to 'Edit', edit_item_path(item) %>
<%= link_to 'Delete', item, method: :delete, data: { confirm: 'Are you sure?' } %>
<%= link_to 'Add Comment', !?????! %>
</td>
/tr>
I have a linked model for 'comments' set up but don't know how to pass the 'item_id' to it when creating a new one.
The URL helpers actually accept the object to make a route for an association. Meaning, assuming you have a nested route for comments within items,
resources :items do
resources :comments
end
you can link_to the new_item_comments_path(item).
The method new_item_comments_path(item) makes a string URL based on the new_item_comments route, which you feed to link_to to make an HTML <a> tag.
To be clearer, in your view you would have:
<%= link_to 'View', item %>
<%= link_to 'Edit', edit_item_path(item) %>
<%= link_to 'Delete', item, method: :delete, data: { confirm: 'Are you sure?' } %>
<%= link_to 'Add Comment', new_item_comments_path(item) #-> (instead of ???) %>
In this case, the item you are passing is the reference to your current item, which allows the URL helper to make a URL for it from the route.
The Rails guide for routing should be a useful read for you.
Now that's assuming your Comment controller assigns the right stuff at the right place. You seemed to have figured that out, but I'll explain for the sake of clarity (and future visitors)
class CommentsController < ApplicationController
# GET /item/:item_id/comments/new
def new
#comment = Comment.new
#item = Item.find(params[:item_id])
#comment.item = #item
# render
end
# POST /item/:item_id/comments
def create
#comment = Comment.new(params[:comment])
#item = Item.find(params[:item_id])
#comment.item = #item
# if #comment.save blah
end
end
All credit should go to #jonallard
The solution is all about routing it seems, you need to pass a url to the form that makes new comments (linked models).
to do this:
Add this (or similar depending on object names) to the page that is calling the creation:
<%= link_to 'Add Comment', new_item_comment_path(#item) %>
In both the new and the create method of the comments_controller there is a line starting #comment = Comment.new. Under that line add:
#item = Item.find(params[:item_id]) AND
#comment.item = #item
edit the top line of the comments template for to: <%= form_for(#comment, {:url => item_comments_path(#item)}) do |f| %>
Edit routes to somethings like:
resources :items do
...
resources :comments
end
and Read this: http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects!
As mentioned before all credit goes to #jonallard, his answer and his expertise.
I am trying to use the link_to feature to link one view to another.
The view i am calling link_to is app/views/instructors/show.html.erb and that snippet of code looks like this (namely, the second to last line of it)
<% provide(:title, #instructor.login) %>
<% courses = Course.where(:instructor_ID => #instructor.id) %>
<div class="span2">
<h1 align=center ><%= #instructor.login %></h1>
<%= link_to "Add course", new_course_path(:instructor_ID\
=> #instructor.id), :class => "btn" %>
<br>
<br>
<%= link_to "Remove course", delete_course_path(courses), :class => "btn"%>
</div>
The view I am trying to link to is is app/views/courses/show_all.html.erb and looks like this:
<% #courses.each do |course| %>
<tr>
<td><%= course.course_name %></td>
<td><%= course.instructor_ID %></td>
<td><%= link_to 'Show', course %></td>
<td><%= link_to 'Edit', edit_course_path(course) %></td>
<td><%= link_to 'Destroy', course, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
</tr>
delete_course_path routes to app/views/courses/show_all.html.erb shown above. When I try the code above, I get the following error:
undefined method `each' for nil:NilClass
At this line:
<% #courses.each do |course| %>
Any ideas what i'm missing in my link_to?
In your show_all action, you should define a #courses instance variables. This is
<% courses = Course.where(:instructor_ID => #instructor.id) %>
not passed to show_all.html.erb.
An instance variables is a variable passed from action of controller to the view corresponding.
I suppose when you show page of instructor, your route will like this: /instructors/:id, so maybe in your show_all action of instructor controller, you need something like:
def show_all
#courses = Course.where(instructor_ID: params[:id])
render 'courses/show_all'
end
This means that #courses is nil. Did you set it in your show_all action of your controller? E.g.
def show_all
#courses = Course.all
end
Also, in your show view, you set courses to a collection of Course objects, but your "Remove course" link looks like you only want to delete one course. Why do you use the delete_course route to link to your show_all view?