qiita.rb
link_to 'Link name', 'Prefix name+ _path'
・ View helper ・ Automatically creates html (creates href)
○ Link name ⇨ Describe that it is displayed as a character string in view
○'Prefix name + _path' ⇨ Use rails routes Prefix (requires _path)
·in this case,'Prefix + _path + (each do | |Value generated by)'Described as ・ Each do| |The value generated by,Generated from instance variables ・ * (Assumption that instance variables have been generated by controller)
qiita.rb
@Instance variable each do|Instance name|
-In this instance name, multiple caches of instance variables are stored. -Convert the value where column name = id to: id in the URL pattern -It means that the: id of the url is described with the argument: id included.
For example
Prefix URL Pattern
edit_question question/:id/edit
-The URL pattern associated with this Prefix includes: id -Therefore, it is necessary to describe (instance name) as well.
qiita.rb
<% @question each do |question| %>
<%= link_to 'Edit', edit_question_path(question) %>
<% end %>
[Explanation] -[: Id,: name,: title,: content] is stored in the question, which is the instance name. ・ Of these,: id is automatically picked up and applied to the URL pattern.
qiita.rb
ridirect_to 'Prefix name+ _path'
-Method to move to the specified URL ・ Basically, the idea is the same as ① link_to method. ・ Differences are described on the Controller -Therefore,'link name' is unnecessary because it is not displayed in view. -Also, the instance variable itself can be used when prefixing the URL pattern. ・(Instance variable each do for description in Class|Instance name|Is unnecessary)
qiita.rb
def action
@Instance variables=Model name.find(params[:id])
redirect_to question_path(@question)
end
[Explanation] ・ Please think that pramasun is a hash name that is summarized separately and pass it through. ・ Get: id from @question and apply it to the URL pattern
qiita.rb
render :html file name
・ Method to skip to URL as in ①② -Note that specify the name of the html file instead of Prefix with: html file name. ・ Basically used in actions -Used when the process at the time of action execution is famous -Return to the path before accessing the path of the action execution condition
qiita.rb
def action
@Instance variables=Model name.find(params[:id])
redirect_to question_path(@question)
end
qiita.rb
def create
@question = Question.new
if @question.save
redirect_to root_path, notice: 'Success!'
else
flash[:alert] = 'Save error!'
render :new
end
end
[Explanation] -Branch the URL that goes after the action is executed into two (redirect_to side or render side) ・ Instantiate @question from Question model ・ Hold the value with @ question.save ・ If the value can be saved correctly here, move to the prefix of redirect_to. ・ If saving fails, return to new.html.erb
○notice: 'Success!' ⇨ When accessing the URL after redirect_to, the user is displayed as'Success!'
○flash[:alert] = 'Save error!' ⇨ "Save error!" Is displayed to the user on the returned view screen.
Recommended Posts