Help with rails link_to and post methods - ruby-on-rails

I need help assigning students to batches.. they are in a many to many relation.
<tbody>
<% Batch.all.each do |b|%>
<tr>
<td><%= b.course.name%></td>
<td><%= b.name %></td>
<td><%= b.section_name%></td>
<td><%= link_to "Add", student_batch_students_path(#student, :batch_id=> b.id), :method=> :post%></td>
</tr>
<%end%>
</tbody>
In my controller
def create
#batch_student = BatchStudent.new(params[:batch_student])
#batch_student.save
end
My routes
resources :students do
resources :batch_students
end
resources :batches
But on my database it creates it with student_id and batch_id as null

You are updating exist batch, but not creating, so you should make PUT request to update action
<td><%= link_to "Add", student_batch_students_path(#student, :batch_id => b.id), :method=> :post %></td>
def create
#student = Student.find(params[:id])
#batch = Batch.find(params[:batch_id])
#batch_student = BatchStudent.new(:params[:batch_student])
#batch_student.student = #student
#batch_student.batch = #batch
#batch_student.save
end

The params hash doesn't contain a :batch_student hash because you are not submitting from a form. The params has should look something like {"student_id" => 1, "batch_id" => 1, "method" => "post"}.
So, modify your create action as follows:
def create
#batch_student = BatchStudent.new(params)
#batch_student.save
end
# or, a shorter version
def create
#batch_student = BatchStudent.create(params)
end
The advantage of using new is you can do a if #batch_student.save to check for errors.
I hope this helps.

The parameters and the http method should be together {:batch_id=> b.id, :method=> :post}
<%= link_to "Add", student_batch_students_path(#student), {:batch_id=> b.id, :method=> :post} %>

Related

creating a button to update a database entry in ruby

I have created a database of ideas with a votes field. I want users to be able to press a button to increase the vote count of an idea and then refresh the screen. I have created a method called increment_vote, but cannot seem to find how to save the new vote value in my database. This is my part of my index.html.erb code:
<% #ideas.each do |idea| %>
<tr>
<td><%= idea.content %></td>
<td><%= increment_vote(idea) %></td>
<td><%= link_to 'Vote', ideas_path(:mode => "Vote"), :class => "button", :method => :get %></td>
</tr>
If I call the increment vote method from the link to vote code, I get an "undefined method `to_model' for true:TrueClass. Did you mean to_yaml" error.
This is my method code in the ideas.controller:
helper_method :increment_vote
def increment_vote(idea)
idea.votes +=1
idea.save
end
This is currently causing the error, but it is increasing the vote of the first idea in the table.
Can anyone please help?
You can't call increment_vote method from view, you need to create controller action for it and call it when the user clicks the link
# views/ideas/index.html.erb
<% #ideas.each do |idea| %>
<tr>
<td><%= idea.content %></td>
<td><%= link_to 'Vote', upvote_idea_path(idea), class: "button", method: :post %></td>
</tr>
<% end %>
# routes.rb
resources :ideas do
post :upvote, on: :member
end
# ideas_controller.rb
def upvote
Idea.find(params[:id]).upvote
redirect_to :index
end
# models/idea.rb
def upvote
update(votes: votes + 1)
end

Passing parameters using link_to

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]) %>

Delete action not working in Rails - Parameter Missing

I'm trying to add a delete action to my app, and I'm getting an odd error that I"m having trouble tracking down. It seems like the create action is being triggered even though I've assigned the button to the delete action. Based on the URL when I click on the delete button, it seems like it might be using GET, which I'm pretty sure isn't correct.
Any help is much appreciated!
Here's the error I'm getting when I click on a delete button in the index view.
class DogsController < ApplicationController
def create
Dog.create(dog_params)
#dogs = Dog.all
redirect_to dogs_path
end
def new
#dog = Dog.new
end
def edit
end
def delete
#dog = Dog.find params[:id]
#dog.destroy
redirect_to dogs_path
end
def show
#dog = Dog.find params[:id]
end
def index
#dogs = Dog.all
end
private
def dog_params
params.require(:dog).permit(:name, :breed)
end
end
And here's the code for the index view:
<h1>List of Dogs</h1>
<table>
<thead>
<tr>
<td>Name</td>
<td>Breed</td>
<td>Details</td>
</tr>
</thead>
<% #dogs.each do |d| %>
<tr>
<td><%= d.name %></td>
<td><%= d.breed %></td>
<td><%= d.id %></td>
<td><%= link_to 'Details', dog_path(d.id) %></td>
<td><%= link_to 'Edit', edit_dog_path(d.id) %></td>
<td><%= button_to 'Delete', :method => :delete %></td>
</tr>
<% end %>
</table>
Rails will be looking for destroy not delete in your controller.
Change def delete to def destroy
Aha also noticed you're not specifying what you want to delete:
<%= button_to 'Delete', d, :method => :delete %>
Also in your create you're getting all Dogs then redirecting which is a waste, remove the #dogs = Dog.all query.
#dogs = Dog.all
redirect_to dogs_path
button_to must pass in a parameter like this
button_to 'Delete', dog, method: :delete

New instance of a linked model, pass 'caller' id

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.

Pass Variable between Views in Rails

I've been trying to figure out how to pass a variable between two views and I've looked at all the examples on stack overflow and I can't seem to make it work.
I have this in my users -> index.html.erb
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= user.id %></td>
<td><%= link_to 'Profile', user %></td>
<td><%= link_to 'Connect', new_relationship_path, :id => user.id %><td>
</tr>
<% end %>
I'm trying to pass user.id to my relationships -> new.html.erb view.
in my controller I have:
class RelationshipsController < ApplicationController
def new
#id = params[:id]
end
end
and finially I have relationships -> new.html.erb
<section id="main">
<h1>Sign Up</h1>
<td><%= #id %></td>
</section>
I believe :id isn't being passed correctly. What am I missing from all the other examples? I get no errors, just nothing is displayed.
This
link_to 'Connect', new_relationship_path, :id => user.id
is passing the :id as an html_option to link_to, so it will be used as an "id" attribute on your link element. What you want instead is to pass it as a parameter to the route helper:
link_to 'Connect', new_relationship_path(:id => user.id)
If users have one or many relationships, it could be smarter to user nested routes.
then you will be able to create relationship for a specific user through a direct url.
eg : new_user_relationship_path(user) # => /user/2134/relationship/new
then in your relationship controller a params[:user_id] would evaluate to 2134
you should look at : http://guides.rubyonrails.org/routing.html

Resources