rails - undefined method `model_name' - ruby-on-rails

I have a basic blogging site in which I want to allow users to offer 'corrections' to posts (just think of it as a comment). The correction object belongs to a post, which in turn belongs to a user (for which I'm using Devise).
I would like the form to create a new correction to be nested in the page for the post,so I'm just rendering the form in posts/show.html.erb with <% render :template => "corrections/new" %>. I'm getting a 'undefined method model_name' error from the line form_for line in corrections/new.html.erb though.
Here's the form:
<% form_for [#correction, :url=> user_post_corrections_path(current_user, #post, #correction)], html: { multipart: true} do |f| %>
<div class="field">
<%= f.label :correction %>
<%= f.text_field :correction %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here's the corrections_controller:
class CorrectionsController < ApplicationController
def new
#post = Post.find(params[:post_id])
#correction = current_user.#post.corrections.build
respond_with(#correction)
end
def create
#post = Post.find(params[:post_id])
#correction = current_user.#post.corrections.build
if #correction.save
redirect_to user_post_path(current_user, #correction.post_id)
end
end
end
In my routes.rb:
resources :users do
resources :posts do
resources :corrections
end
end
Any help with this would be much appreciated.

There are several issues I can see, first off:
The form is not rendered:
The form for should be evaluated not computed so you should use <%= %> tag instead of <%%> tag, so it becomes:
<%= form_for #correction, url: user_post_corrections_path(current_user, #post, #correction), html: { multipart: true} do |f| %>
Second: You are saying form_for #correction, and you said this form is shown in posts/show, this means that the show action for the post should have the following in its controller:
#correction = Correction.new user: current_user, post: #post
This is assuming you are using the #post variable for the post show meaning you have something like
#post = Post.find params[:id]
This line is completely off
#correction = current_user.#post.corrections.build
you can just say:
#correction = Correction.new correction_params
#correction.user = current_user
#correction.post = #post
if #correction.save
redirect_to user_post_path(current_user, #correction.post_id)
end
The important part is your show action on the posts controller having the initialization of your #correction object.

This line looks off:
#correction = current_user.#post.corrections.build
#post is an instance variable, not a method on current_user
Did you intend to build a correct with the current user? The following should work:
#post.corrections.build(user: current_user)
If your corrections model has_many :users, through: :posts, then the following should work:
#post.corrections.build(users: [current_user])
EDIT
The format of your form_for is also incorrect. The url: key in form_for doesn't belong in the resource array (the first arg in your form_for)
The following modification should resolve the error, provided user_post_corrections_path takes :id, :post_id, and :correction_id
form_for #correction, url: user_post_corrections_path(current_user, #post, #correction), html: { multipart: true} do |f|
I'd also reduce your routing to two levels of nesting if possible.
Perhaps this is easier?
resources :posts do
resources :corrections
end
end
If it's implied a post belongs to the current_user then it may not be necessary to have /users in the route path.

Related

No route matches [PATCH] "/persons"

I'm new to the ruby. He saw such a mistake, he looked through a lot of articles, I do everything exactly as instructed, but nothing comes out. With Create everything is in order but on the update swears.
my action:
...
def edit
#person = Person.find(params[:id])
end
def update
#person = Person.find(params[:id])
#person.update_attributes(person_params)
if #person.errors.empty?
redirect_to #person
else
render 'edit'
end
end
private
def person_params
params.require(:person).permit(:number, :family, :name, :patronymic, :other)
end
my views:
<h1>Редактировать личность</h1>
<%= form_for #person, url: persons_path do |f| %>
<p>Номер</p>
<p><%= f.text_field :number %></p>
...
<p><%= f.submit "изменить базу" %></p>
<% end %>
and router:
Rails.application.routes.draw do
resources :persons
end
You don't need to pass , url: persons_path, since persons is resource, form_for #person will figure out that it has to go to update action.
<%= form_for #person do |f| %>
should be fine.
If you still do need to pass the url, it should be url: person_path(#person), html: {method: "patch"})
Read more about this topic in Rails Edge guide -
http://guides.rubyonrails.org/v4.1/form_helpers.html#relying-on-record-identification
Note: As #engineersmnky also pointed, your config/routes.rb should traditionally have resources :people instead. Resource is specified in pluralized form.
If you generate scaffold with Rails - rails g scaffold People name:string, you will see that you get:
model - person.rb
controller - people_controller.rb
route helper - resources :people

Rails form rendering wrong URL after validation errors (not keeping passed parameter)

On a project show page, I pass a very simple parameter on my 'create new task' that stores which project its from:
#project.id), :class => "btn btn-info col-md-12" %>
so that when i create a new task for it, it stores it in the URL on my new task form like this:
http://localhost:3000/task/new?project_id=5
My New form is as follows:
<div class="container sign-in-register">
<div class="authform">
<%= form_for #task, :html => {:multipart => true} do |f| %>
<h3>Add a task for this project...</h3><br/>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= hidden_field_tag 'project_id', #project_id %>
<%= f.fields_for :taskrelationships do |ff| %>
<%= ff.hidden_field :taskproject_id, value: #project_id %>
<%= ff.label :task_url %>
<%= ff.text_field :task_url, class: 'form-control' %>
<% end %>
<br clear="all">
<%= f.submit "Save Task", class: "btn btn btn-info" %>
<% end %>
</div>
</div>
As you can see i'm using nested attributes in the form (I am creating both a task and a TaskRelationship. Now, when I try to save without filling out all the required fields a validation is thrown but for some reason it redirects me to:
http://localhost:3000/tasks
instead of the original:
http://localhost:3000/tasks/new?project_id=5
I have read many posts and none seem to answer this particular case. The stackO post below was close but when I try it with task instead of user it still cannot find the task_ID
Render error on new page with customize url like http://localhost:3000/education_informations/new?user_id=10
How can I have rails simply render the same exact url as I started with - it seems like this should be easy so must be missing something small.
My controller actions:
def new
#project_id = params[:project_id]
#task = Task.new
#task.taskrelationships.build
end
def create
#project = Project.find(params[:project_id])
#task = Task.new(task_params)
if #task.save
flash[:success] = "This task has been added."
#task.taskrelationships.create!(#taskrelationships_params)
redirect_to tasks_project_path(#project)
else
#task.taskrelationships.build(#taskrelationships_params)
flash[:alert] = #task.errors.full_messages.to_sentence
render :new
end
end
private
def task_params
#taskrelationships_params = params.require(:task).permit(taskrelationships_attributes: [
:task_url,
:taskproject_id
])[:taskrelationships_attributes]["0"]
params[:task].delete(:taskrelationships_attributes)
params.require(:task).permit(
:name,
:user_id,
taskrelationships_attributes: [
:task_url,
:taskproject_id
]
).merge(owner: current_user)
end
UPDATE W / ROUTES
resources :projects do
resources :reviews, except: [:destroy]
member do
get :tasks
end
end
resources :tasks
resources :taskrelationships, only: [:create, :destroy] do
post :vote, on: :member, controller: "task_relationships"
end
thanks for any assistance...
Ok firstly an explanation as to what is going on here:
When you invoke http://localhost:3000/task/new?project_id=5 you are actually being routed to the new action on the task controller (with a project_id param).
Your new action then sets the variables and rails will render the new.html.erb which contains your new task form.
When you submit the form it actually is doing a http POST to /tasks, which routes to the create action of your tasks controller. That url and http method is a result of what is generated from the form_for helper:
<%= form_for #task, :html => {:multipart => true} do |f| %>
This is why the url changes from /tasks/new?project_id=5 to /tasks
Now the create action if it fails the validation simply renders the new form - it is not redirecting anywhere - the url remains unchanged from what it was when it entered this action - meaning, it remains as /tasks.
You do not actually need to navigate to /tasks/new?project_id=5 to render the new form but what you do need to do is set #project_id in the controller so the view has access to that variable (just as it does in the new action):
def create
#project = Project.find(params[:project_id])
#task = Task.new(task_params)
if #task.save
flash[:success] = "This task has been added."
#task.taskrelationships.create!(#taskrelationships_params)
redirect_to tasks_project_path(#project)
else
#task.taskrelationships.build(#taskrelationships_params)
#project_id = #project.id
flash[:alert] = #task.errors.full_messages.to_sentence
render :new
end
end
So, to clarify the change in url is not a redirection it's just that the form is posting to a different url than /tasks/new, and this is actually just a cosmetic issue.
Now if it is a concern to you, you can change the routing to something like the following:
resources :tasks, except: [:create, :new]
post 'new_task' => 'tasks#create'
get 'new_task' => 'tasks#new'
This is mapping the POST and GET http methods to /new_task so the url appears the same for the new and create action invocations. Note you do need to change the url in the form_for helper to use this new route:
<%= form_for #task, url: 'new_task', multipart: true do |f| %>
Since Rails default behaviour in forms is with remote: true, you can move the content of the form to a partial(let's name it _my_form.html.erb), add to the controller action (let's say in create action):
respond_to do |format|
format.js {}
end
Then add a create.js.erb file where you will render the form partial
$("#form").html(
"<%= j render partial: 'my_form', locals: { entity: #entity } %>"
);
Thus, validation errors and all attributes will be accessible inside the form and there is no need to hack the "Rails approach"
I had to adapt and experiment with the currently accepted answer,
and the following ended up working well (using schools rather than tasks):
In config/routes.rb:
resources :schools do
...
end
post 'schools/new' => 'schools#create'
And create the form like:
= simple_form_for [#school], url: 'new' do |f|
Thus the path /schools/new was preserved on validation :)

Showing form error messages

I'm having trouble getting my redirect and error messages to work. From what I've read you cant get a forms errors to show up when you use redirect so I am trying to use render after it fails.
I have a new post form on a topic page. The url is "topic/1". If you make a post about the topic and something is wrong with the input I want it to go back to the page at topic/1 and display errors and I cant figure out how to get it to go back. Redirect (:back) does what I want but doesnt show the forms errors.
The form on the topic's show.html page:
<%= form_for(#post) do |f| %>
<%= render 'shared/post_error_messages' %>
<%= f.label :title, "Post Title" %>
<%= f.text_field :title %>
<%= f.label :content %>
<%= f.text_field :content %>
<%= f.hidden_field :parent_id, value: 0 %>
<%= f.hidden_field :topic_id, value: #topic.id %>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.submit "Create Post" , class: "btn btn-small btn-primary" %>
<% end %>
Create action in the Posts controller
def create
#post = Post.new(post_params)
#topic = Topic.find_by(id: params[:topic_id])
if #post.save
redirect_to #post
else
#topic = Topic.new
render "/topics/show"
end
end
I guess I'm mostly trying to do the render with the id from the page that the form was originally on.
Errors
The problem isn't anything to do with the way you're rendering the form (render or redirect) - it's to do with the way you're handling your ActiveRecord object.
When you use form_for, Rails will append any errors into the #active_record_object.errors method. This will allow you to call the following:
form_for error messages in Ruby on Rails
<%= form_for #object do |f| %>
<% #location.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
<% end %>
This only works if you correctly create your ActiveRecord object, which you seem to do
--
Nested
#config/routes.rb
resources :topics do
resources :posts, path: "", path_names: {new: ""}, except: [:index] #-> domain.com/topics/1
end
You'll be much better using the following setup for a nested route:
<%= form_for [#topic, #post] do |f| %>
...
<% end %>
This allows you to create a form which will route to the topics_posts_path, which is basically what you need. The controller will then balance that by using the following:
#app/controllers/topics_controller.rb
Class TopicsController < ApplicationController
def new
#topic = Topic.find params[:topic_id]
#post = Post.new
end
def create
#topic = Topic.find params[:topic_id]
#post = Post.new post_params
end
private
def post_params
params.require(:post).permit(:attributes)
end
end
You are overwriting the Topic you original found with a brand new, empty one - which shouldn't be necessary and which is causing the posts related to it to disappear.
Also - if your topic and post are related - you should create the post on the appropriate association #topic.posts instead of the main Post class.
The #topic.posts.new means that the post's topic-id is automatically updated with the value of the #topic.id ... which means you don't need to set it in the hidden-field on the form.
In fact it's better if you don't - just delete that hidden field entirely.
If you add that to the first time you get a new post too (eg in topics/show) then you won't need to pass in a value to the hidden-field.
Also I'd do the same for all the other hidden-fields on the server-side too. You don't really want the user to use firebug to hack the form and add some other user's id... so do it in the create action and don't bother with the hidden field
This should work:
def create
#topic = Topic.find_by(id: params[:topic_id])
#post = #topic.posts.new(post_params)
#post.user = current_user
#post.parent_id = 0
if #post.save
redirect_to #post
else
render "/topics/show"
end
end
if it doesn't - let me know what error messages you get (so we can debug)

Rails 4 Routing Error - no rout matches [POST] "/categories/new"

While learning Rails 4 I stuck while trying to add categories to posts of my simple blog. I have generated model, ran the migration and added a controller. No matter what I do now when trying to create a category, I keep running into same mistake: no route matches [POST], which is weird, as I seem to have all the code in place. Please help!
categories controller
class CategoriesController < ApplicationController
def index
#categories = Category.all
end
def new
#category = Category.new
end
def create
#category = Category.new(category_params)
#category.save
redirect_to new_category_path, alert: "Category created!"
end
def show
#category = Category.find(params[:id])
end
def destroy
#category = Category.find(params[:id])
#category.destroy
redirect_to categories_path
end
private
def category_params
params.require(:category).permit(:name)
end
end
routes.rb
Blog::Application.routes.draw do
get 'tags/:tag', to: 'posts#index', as: :tag
resources :categories
resources :posts do
resources :comments
end
root 'welcome#index'
end
category.rb
class Category < ActiveRecord::Base
validates :name, presence: true
has_many :posts
end
new.html.erb
<%= form_for :category do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
/categories/new
No route matches [POST] "/categories/new"
You should have in your view
<%= form_for #category do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
#category object is used by form_for method to figure out form url.
If you pass only Symbol to form_for method, without specifying url explicitly, form will be generated with url being the current url.
Although the #category works, if you read a bit further you will see that they will explain why the your code sends No route matches [POST] "/categories/new".
The guide actually explains that you need to specify the url: posts_path for the form to use the right route.
There's one problem with this form though. If you inspect the HTML
that is generated, by viewing the source of the page, you will see
that the action attribute for the form is pointing at /posts/new. This
is a problem because this route goes to the very page that you're on
right at the moment, and that route should only be used to display the
form for a new post.
The form needs to use a different URL in order to go somewhere else.
This can be done quite simply with the :url option of form_for.
Typically in Rails, the action that is used for new form submissions
like this is called "create", and so the form should be pointed to
that action.
Edit the form_for line inside app/views/posts/new.html.erb to look
like this:
<%= form_for :post, url: posts_path do |f| %>
In this example, the
posts_path helper is passed to the :url option. What Rails will do
with this is that it will point the form to the create action of the
current controller, the PostsController, and will send a POST request
to that route.

Rails Associations (belongs_to, has_many) can't save 2 ids in table with a create method (user, post, comment)

Trying to write a basic "blog-like" app in rails 3, I'm stuck with associations. I need the create method save the post_id as well as the user_id in the comment table (which I need in order to retrive all comments written by a user in order to display it)
The app has users (authentication - devise), posts (posted by users - but I'm not sure it matters in my case) and comments (on the posts, posted by users).
the comment table has a post_id, a body, and also a user_id
Associations:
has_many :comments (In the Post model)
belongs_to :post (In the Comment model)
belongs_to :user (In the Comment model)
has_many :comments (In the User model)
the routes:
resources :posts do
resources :comments
end
resources :users do
resources :comments
end
The comment post form displayed on the posts show view: (posts/show.html.erb)
<% form_for [#post, Comment.new] do |f| %>
<%= f.label :body %>
<%= f.text_area :body %>
<%= f.submit %>
<% end %>
and finally, the create method in the comments controller:
A.) If I write this a post_id is written in the database
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create!(params[:comment])
redirect_to #post
end
B.) If I write this a user_id is written...
def create
#user = current_user
#comment = #user.comments.create!(params[:comment])
redirect_to #post
end
I tried:
#comment = #post.comments.create!(params[:comment].merge(:user => current_user))
But it doesn't work.. How can I write a method which save the user_id and the post_id ? Did I have also to do some change in the comment post form (something like <% form_for [#post, #user, Comment.new] do |f| %> ?)
Thank you!
To set up something very similar, I've used the following form:
<%= form_for [:place, #comment] do |f| %>
#form fields here
<%= end %>
Then in the comments controller:
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.build(params[:comment])
#comment.user = User.find(current_user.id)
respond_to do |format|
if #comment.save
format.html { redirect_to(#comment.post, :notice => 'Comment was successfully created.') }
else
format.html { render :action => "new" }
end
end
end
That should build the associations properly hopefully! Just as an aside, do you mean for comments to be nested under :users in your routes? If you just want to display all the user's comments on a profile page, you could do something like:
<p>
<b>Comments</b>
<% if #user.comments.empty? %>
No comments to display yet...
<% else %>
<% #user.comments.each do |comment| %>
<p>
<%= link_to "#{comment.post.title}", post_path(comment.post_id) %>, <%= comment.created_at %>
<%= simple_format comment.content %>
</p>
<% end %>
<% end %>
</p>
Hope some of that helps!

Resources