How to pass data from view to controller in rails - ruby-on-rails

I'd like to actualise the function that current_user answers a question. In order to achieve it, I need to pass question_id data from view to controller.
In particular,
1) Pass question_id from views/homes/popular.html.erb to answers_controller.rb
2) Pass question_id from answers_controller.rb to views/answers/new.html.erb
3) Pass question_id from views/answers/new.html.erb to answers_controller.rb
I'd like to pass question_id to hogehoge at answers_controller.rb. Could you tell me how to do it?
#app/controllers/homes_controller.rb
class HomesController < ApplicationController
def popular
#questions = Question.all
#questions.each do |question|
#answer = question.answers.highest
end
end
end
#app/views/homes/popular.html.erb
<div class="row">
<div class="row">
<h3>Popular</h3>
<% #questions.each do |question| %>
<li><%= link_to question.body, new_answer_path ,question_id:question.id%></li>
<% end %>
</div>
</div>
#app/controllers/answers_controller.rb
class AnswersController < ApplicationController
def new
#answer = Answer.new
end
def create
#answer = current_user.answers.create(answer_params)
if #answer.save
redirect_to root_url
else
render 'new'
end
end
private
def answer_params
hogehoge
end
end
#app/views/answers/new.html.erb
<%= form_for #answer, :url => answers_path do |f| %>
<div class="page-header">
<h2>Please answer</h2>
</div>
<div class="span6 offset3">
<%= f.label :body %><br>
<%= f.text_field :body %>
</div>
<%= f.submit 'post', class: 'btn btn-primary' %>
<% end %>

To send data from view to controller you can use query string as you used in popular.html.erb and in answer controller you get data in params[:question_id] , use instance variable in new action as #question = params[:question_id] and in answer/new.html use hidden field f.hidden_field :#question to send data.

Related

while Form sumbitting extra blank item

When I submit an answer form, the first item is getting the only empty string, how I can fix it and guide me.
Extra item picture:
question model
class Question < ApplicationRecord
has_many :answers, dependent: :destroy
end
answer model
class Answer < ApplicationRecord
belongs_to :question
end
answer controller
class AnswersController < ApplicationController
def create
#question = Question.find(params[:question_id])
#answer = #question.answers.create(params[:answer].permit(:body))
redirect_to question_path(#question)
end
def destroy
#question = Question.find(params[:question_id])
#answer = #question.answers.find(params[:id])
#answer.destroy
redirect_to question_path(#question)
end
end
answer form
<%= form_for([#question,#question.answers.build]) do |f| %>
<div class="row">
<div class="input-field col s12 m9">
<%= f.text_area :body ,:id=>"ans_form" %>
<label for="first_name"style="text-indent:15px;line-height:15px">Create Possible Answer</label>
</div>
<div class="input-field col s12 m3">
<%= button_tag(type: 'submit', class: "col s12 btn waves-effect accent-4") do %>
Create Answer
<% end %>
</div>
</div>
<% end %>
in my view (show page )
<%= #question.answers.each do |answer| %>
<%= answer.body %>
<%= link_to 'delete',[answer.question, answer], method: :delete %>
<% end %>

Accessing Model ID inside of rails form helper

First off I'm new to rails, I have a join table Menus_orders which I'm trying to load ID's into on create.
I have this form helper which has access to Order.new(:id, :name) fields and fields_for Menus_order(:menu_id, :order_id). I can insert the menu_id from a select but I can't for the life of me figure out how to populate a hidden input with the current order_id which the form holds in #order.
look at ORDER_ID HERE
<div class="form">
<h1>Create a new order</h1>
<%= form_for #order do |f| %>
<%= f.label :name%><br>
<%= f.text_field :name%><br>
<h3>Select meals</h3>
<%= fields_for(#menus_order) do |i| %>
<div class="field">
<%= i.select :menu_id, options_for_select(Menu.all.pluck(:id)) %>
<%= i.hidden_field :order_id, :value => ORDER_ID HERE %>
</div>
<% end %>
<br>
<%= f.submit "Create"%><br>
<% end %>
</div>
Thanks, DW
You should use nested attributes for order model just apply
the conventions right & it will be handled, you can achieve this by
following example :
order.rb should be like :
class Order < ActiveRecord::Base
has_many :menu_orders
accepts_nested_attributes_for :menu_orders
end
menu_order.rb be like :
class MenuOrder < ActiveRecord::Base
belongs_to :order
end
orders/new.html
<div class="form">
<h1>Create a new order</h1>
<%= form_for #order do |f| %>
<%= f.label :name%><br>
<%= f.text_field :name%><br>
<h3>Select meals</h3>
<%= f.fields_for :menu_orders do |i| %>
<div class="field">
<%= i.select :menu_id, options_for_select(Menu.all.pluck(:id)) %>
</div>
<% end %>
<br>
<%= f.submit "Create"%><br>
<% end %>
</div>
OrdersController
class OrdersController < ApplicationController
def new
#order = Order.new
#order.menu_orders.build
end
def create
#order = Order.new(order_params)
if #order.save
redirect_to orders_path
flash[:success] = "Order created"
else
render 'new'
end
end
private
def order_params
params.require(:order).permit(:name, menu_orders_attributes: [ :menu_id, :order_id ])
end
end

Adding Comments to multiple models

Im trying to add comments to my topics model the same way you can add comments to posts on my app. i currently have to partials for comments _comment.html.erb and _form.html.erb
_comment :
<%= content_tag :div, class: 'media', id: "comment-#{comment.id}" do %>
<div class= "media">
<div class= "media-body">
<small>
<%= comment.user.name %> commented <%= time_ago_in_words(comment.created_at) %> ago
<% if user_is_authorized_for_comment?(comment) %>
| <%= link_to "Delete", [comment.post, comment], method: :delete %>
<% end %>
</small>
<p> <%= comment.body %></p>
</div>
</div>
<% end %>
_form :
<h4>Add a comment</h4>
<%= form_for [post, comment] do |f| %>
<div class="form-group">
<%= f.label :body, class: 'sr-only' %>
<%= f.text_field :body, class: 'form-control', placeholder: "Enter a new comment" %>
</div>
<%= f.submit "Submit Comment", class: 'btn btn-default pull-right' %>
<% end %>
my topic show is :
#DISPLAY Topic comments here
<h3> Comments</h3>
<%= render #topic.comments %>
</div>
<% if current_user %>
<%= render 'comments/form', comment: Comment.new, post: #post %>
<% end %>
#------
comment controller :
def create
#post = Post.find(params[:post_id])
comment = #post.comments.new(comment_params)
comment.user = current_user
if comment.save
flash[:notice] = "Comment saved successfully."
redirect_to [#post.topic, #post]
else
flash[:alert] = "Comment failed to save."
redirect_to [#post.topic, #post]
end
end
def destroy
#post = Post.find(params[:post_id])
comment = #post.comments.find(params[:id])
if comment.destroy
flash[:notice] = "Comment was deleted"
redirect_to [#post.topic, #post]
end
end
i have updated the routes for topic comments :
resources :topics do
resources :posts, except: [:index]
resources :comments, only: [:create, :destroy]
end
my question is do i need to create a separate partial to add comments to topics or can i update my _comment partial to work for both post and topic comments . and how can i accomplish this ?
Models
You'll need a polymorphic association on the Comment model:
#app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end
#app/models/topic.rb
class Topic < ActiveRecord::Base
has_many :comments, as: :commentable
end
#app/models/post.rb
class Post < ActiveRecord::Base
has_many :comments, as: :commentable
end
Controllers
This will allow you to save the comments for your various models, the controllers / flow coming secondary:
#config/routes.rb
resources :topics, :posts do
resources :comments, only: [:create, :destroy] #-> url.com/topics/:topic_id/comments
end
#app/controllers/comments_controller.rb
class CommentsController < ApplicationController
def create
id = params[:post_id] || params[:topic_id]
if params[:post_id]
#parent = Post.find id
elsif params[:topic_id]
#parent = Topic.find id
end
#comment = #parent.comments.find params[:id]
#comment.save
end
def destroy
#parent = params[:post_id] || params[:topic_id]
#comment = #parent.comments.new comment_params
#comment.destroy
end
private
def comment_params
params.require(:comment).permit(:x, :y)
end
end
Because you're passing the data to the comments controller, you'll only need to evaluate which #parent you're working with.
Views
For your views, you need to pass locals to your _form partial:
#app/views/posts/show.html.erb
<%= render "comments/form", locals: {parent: #post} %>
#app/views/comments/_form.html.erb
<%= form_for [parent, parent.comments.new] do |f| %>
I had this road-block as well and here is what I came up with that passed. I must first give #Richard Peck applause for getting my wheels turning, so thank you :).
Models
Did not implement a polymorphic association. Stuck with has_many and belongs_to, nothing more
Partials
_comment.html.erb
Set up the delete partial to accept "parent" as a local
<div class="media">
<div class="media-body">
<small>
<%= comment.user.name %>
commented
<%= time_ago_in_words(comment.created_at) %>
ago
<% if user_is_authorized_for_comment_via_post?(comment) %>
|
<%= link_to "Delete", [parent, comment], method: :delete %>
<% end %>
</small>
<p>
<%= comment.body %>
</p>
</div>
</div>
_form.html.erb
same idea as _comment.html.erb, see above
<h4>Add a comment</h4>
<%= form_for [parent, comment] do |f| %>
<div class="form-group">
<%= f.label :body, class: 'sr-only' %>
<%= f.text_field :body, class: 'form-control', placeholder: "Enter a new comment" %>
</div>
<%= f.submit "Submit Comment", class: 'btn btn-default pull-right' %>
<% end %>
Setting up CommentController
...
def create
# upon clicking on create, determine what param id is passed
if params[:post_id]
# if it is a post id, set instance of post id as #parent
#parent = Post.find(params[:post_id])
elsif params[:topic_id]
# if it is a topic id, set instance of topic id as #parent
#parent = Topic.find(params[:topic_id])
end
# create instance as #comment. Build/create
# comment belonging to #parent (Topic or Post)
#comment = #parent.comments.build(comment_params)
# The comment must be associated to the current user.
# A comment must have a user, and value of user within instance of #comment
# is currently nil. Set user id as current user
#comment.user = current_user
# save comment to database
if #comment.save
# direction of save through if and elsif
# Redirection depends on the comment's parent.
# .is_a? method determines if it is of a certain class. Here, is #parent
# of class Post? Is #parents is the same parent id passed through params?
if #parent.is_a?(Post) # template error with this included: (== params[:post_id])
flash[:notice] = 'Comment saved successfully'
redirect_to [#parent.topic, #parent]
# if not part of the class Post, is it a Topic? If so, save here and
# redirect to the topic after save
elsif #parent.is_a?(Topic)
flash[:notice] = 'Comment saved successfully'
redirect_to #parent
end
end
end
def destroy
comment = Comment.find(params[:id])
# #topic = Topic.find(params[:topic_id])
# topic_comment = #topic.comments.find(params[:id])
# #post = Post.find(params[:post_id])
# post_comment = #post.comments.find(params[:id])
if comment.destroy
flash[:notice] = 'Comment was deleted'
redirect_to :back
else
flash[:alert] = "Comment counld't be deleted. Try again"
redirect_to :back
end
end
...
Passing in Comments from topic/show and post/show
topic/show
Note: notice how locals are passed into the controller from here
...
<div class="row">
<%= render 'comments/form', comment: Comment.new, parent: #topic %>
</div>
<% #topic.comments.each do |comment| %>
<%= render partial: 'comments/comment', locals: { parent: #topic, comment: comment } %>
<% end %>
...
post/show
<% if current_user %>
<% #post.comments.each do |comment| %>
<%= render partial: 'comments/comment', locals: { parent: #post, comment: comment } %>
<% end %>
<% end %>
<% if current_user %>
<%= render 'comments/form', comment: Comment.new, parent: #post %>
<% end %>
Hope this helps.

Variables not displaying in rails blog post

I'm building a simple blog tool using rails and having trouble with variables displaying from a form. What is most confusing is that the post times are showing correctly, but the title and text aren't coming through.
My new form page looks like:
<h1>New post</h1>
<%= form_for :blog, url: blogs_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
The controller is currently:
class BlogsController < ApplicationController
def index
end
def new
end
def create
#blog = Blog.new(post_params[:id])
#blog.save
redirect_to #blog
end
def show
#blog = Blog.find(params[:id])
end
private
def post_params
params.require(:blog).permit(:title, :body)
end
end
And the show page is:
<div id="blog">
<h1>
<%= #blog.title %>
</h1>
<p class="date">
Submitted <%= time_ago_in_words(#blog.created_at) %> ago
</p>
<p>
<%= #blog.body %>
</p>
</div>
Any ideas?
Change:
def create
#blog = Blog.new(post_params[:id])
#blog.save
redirect_to #blog
end
to:
def create
#blog = Blog.new(post_params)
#blog.save
redirect_to #blog
end
The reason why it wasn't working is that you were only trying to savepost_params[:id]. You need to pass the whole param as an argument when creating a new blog post Blog.new(post_params)

Rails Error "expected ...model.rb to define Model"

class ItemController < ApplicationController
def create
item = current_user.items.build(params[:presentstem])
item.created_at = Time.now
item.save!
redirect_to root_path
end
def destroy
end
end
And my form in views/home/index/html.erb to add an item
<div id="add_item">
<p>Add a new item</p>
<% form_for Item.new do |f| %>
<div id="add_item_container">
<%= f.text_field :present %>
<%= f.text_field :stem %>
<%= f.text_field :secondary %>
<%= f.check_box :atype %>
<%= f.text_field :comment %>
</div>
<%= f.submit "Add to List" %>
<% end %>
</div>
How do I define Item?
at localhost:3000 I get
Expected /Users/user/Desktop/test/app/models/item.rb to define Item
Extracted source (around line #3):
You should have Item class definition in this file /Users/user/Desktop/test/app/models/item.rb, probably you don't...
class Item < ActiveRecord::Base
#class definition goes here
end
You are thinking wrong.
You have an model in app/models/item.rb
for this you have an controller in app/controllers/items_controller.rb
and you have views in app/views/items/template.haml
if you want to do a form_for you do the form for a object. rails is looking what type of action you want to participate (new, update) and generates automatically the route (restful).
so you just gave an object to the form_for helper
#in view
=form_for Item.new do |f|
or
#in items_controller.rb
def new
#item = Item.new
end
#in new.haml
=form_for #item

Resources