I am practicing the posts in rails guide. In comments controller I write like this but it comes to error
class CommentsController < ApplicationController
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(params[:comment].permit(:commenter, :body))
redirect_to post_path(#post)
end
end
I'd recommend to follow up this guide
This should work:
class CommentsController < ApplicationController
def create
#article = Article.find(params[:article_id])
#comment = #article.comments.create(comment_params)
redirect_to article_path(#article)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
Related
rails (5.2.2.1)
ruby 2.5.0p0
Parent controller of Country, State, City
class LocalityController < ApplicationController
def create
locality = model_name.new(locality_master_params)
respond_to do |format|
if locality.save
format.html { redirect_to locality, notice: 'Record was successfully created.' }
else
format.html { render :new }
end
end
end
private
def model_name
"#{controller_name.titleize.delete(' ').singularize}".constantize
end
def locality_params
#locality_params = %i|name code status|
end
def locality_master_params
params.require("#{controller_name.singularize}".to_sym).permit(locality_params)
end
end
State controller
class StateMastersController < LocalityController
alias_method :state_master_params, :locality_master_params
private
def locality_params
#locality_params = %i|name code status country_code|
end
end
Expectation: country-state-city controllers should be inherited from one controller and manage same templates, methods for all those controllers to DRY.
This code works fine as per the expectation.
Issue: after removing below code(as it is unnecessary):
alias_method :state_master_params, :locality_master_params
it gives error as:
ActiveModel::ForbiddenAttributesError
I've added alias_method to prevent above error.
Getting same error in other controllers too: country-city controllers.
Is there any convention to define method as state_master_params for state_master_controller?
`
I'm new to learning Rails 3 and working through a Q&A app tutorial. I'm just wondering why I can't do this (I get an error) in relating a particular answer to a question. It works for the current user...
class AnswersController < ApplicationController
before_filter :auth, only: [:create]
def create
#question = Question.find(params[:question_id])
**#answer = Answer.new(params[:answer])
#answer.question = #question
#answer.user = current_user**
if #answer.save
flash[:success] = 'Your answer has been posted!'
redirect_to #question
else
#question = Question.find(params[:question_id])
render 'questions/show'
end
end
end
The tutorial says that this is the correct way:
class AnswersController < ApplicationController
before_filter :auth, only: [:create]
def create
#question = Question.find(params[:question_id])
**#answer = #question.answers.build(params[:answer])**
#answer.user = current_user
if #answer.save
flash[:success] = 'Your answer has been posted!'
redirect_to #question
else
#question = Question.find(params[:question_id])
render 'questions/show'
end
end
end
Doing the following
#answer = #question.answers.build(params[:answer)
Is the same as doing this
#answer = Answer.new(params[:answer])
#answer.question_id = #question.id
Doing a build adds the relation attributes to the new answer, in this case question_id
As for the error, can you provide the type of error you receive?
I want to get all the microposts where 'something' = true.
This code works fine
class UsersController < ApplicationController
.
.
.
def show
#user = User.find(params[:id])
#microposts = #user.microposts
#titre = #user.nom
end
end
But when I tried to make a where sql method this code doesn't work.
class UsersController < ApplicationController
.
.
.
def show
#user = User.find(params[:id])
#microposts = #user.microposts.where("something = 'true'")
#titre = #user.nom
end
end
any idea ?
def show
#user = User.find(params[:id])
#microposts = #user.microposts.where(something: true)
#titre = #user.nom
end
See here or here for more info on narrowing the query scope.
Write like this:
Micropost.where(user_id: params[:id], something: true)
I am trying to write a forum with Ruby on Rails.
On model side, I finished association between Topic and Forum
# forum.rb
class Forum < ActiveRecord::Base
has_many :topics
attr_accessible :name, :description
end
# topic.rb
class Topic < ActiveRecord::Base
has_many :posts
belongs_to :forum
end
Controller for Forum
# forums_controller.rb
class ForumsController < ApplicationController
def new
#forum = Forum.new
end
def create
#forum = Forum.new(params[:forum])
if #forum.save
flash[:success] = "Success!"
redirect_to #forum
else
render 'new'
end
end
def index
#forums = Forum.all
end
def show
#forum = Forum.find(params[:id])
end
end
Controller for Topic
class TopicsController < ApplicationController
def new
#topic = current_forum???.topics.build
end
def create
#topic = Topic.new(params[:topic])
if #topic.save
flash[:success] = "Success!"
redirect_to #topic
else
render 'new'
end
end
def index
#topics = Topic.all
end
def show
#topic = Topic.find(params[:id])
end
end
How do I change new and create for topics_controller to make sure the topic is created for current forum rather than some other one?
So for example, if I create a new topic from a forum with id=1, how do I make sure that forum_id=1 for the new topic created?
Using nested resources
resources :forums do
resources :topics
end
you will have a path like
/forums/:forum_id/topics/new
then in your TopicsController
def new
#forum = Forum.find(params[:forum_id])
#topic = #forum.topics.build
end
class TopicsController < ApplicationController
def new
#forum = Forum.find(params[:id])
#topic = #forum.topics.build
end
Am trying to find a way of capitalizing the 1st letter of all Titles and Tags when a user submits an article. I can use the capitalize method, but where do I add it to the controller code blocks for it to work?
Thx
controllers/articles_controller:
def new
#article = Article.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #article }
end
end
controllers/tags_controller:
class TagsController < ApplicationController
def show
#tag = Tag.find(params[:id])
#articles = #tag.articles
end
end
models/article:
def tag_names
#tag_names || tags.map(&:name).join(' ')
end
private
def assign_tags
if #tag_names
self.tags = #tag_names.split(/\,/).map do |name|
Tag.find_or_create_by_name(name)
end
end
...
Where do you plan to capitalize it? before saving in the database? or when you're showing it to the user?
There are two ways to this:
Use rail's titleize function or capitalize
or do it using CSS with:
<p class="tag">im a tag</p>
#CSS
.tag {
text-transform:capitalize;
}
I would do something like this to force them to be capitalized before saving.
class Article < ActiveRecord::Base
def title=(title)
write_attribute(:title, title.titleize)
end
private
def assign_tags
if #tag_names
self.tags = #tag_names.split(/\,/).map do |name|
Tag.find_or_create_by_name(name.capitalize)
end
end
end
end
Try to use .capitalize
e.g. "title".capitalize will make "Title"
As Francis said, use .capitalize in your controller
I use this
#article= Article.new(params[:article])
#article.name = #article.title.capitalize
#article.save