复选框以从显示视图更新布尔值

时间:2014-10-23 12:32:20

标签: ruby-on-rails checkbox view

有一个问题模型和一个Note模型。问题可以有许多注释。

问题显示视图还显示与该问题相关的注释。我想在注释旁边有一个“解决方案”复选框,如果该注释可以解决问题,可以勾选。然后,这将更新notes表中的布尔字段is_solution。如果失败(notes_params),我目前的代码为:note node为nil。感激不尽。

show.html.erb

<% @problem.notes.each do | note | %>
      <tr>
      <td>    <%= form_tag (problem_notes_solution_path(@problem, note)) do %>
                 <%= markdown(note.body) %><%= check_box_tag "is_solution", note.is_solution %>          
                 <%= submit_tag "Mark as Solved" %> 
                 <% end %></td>
 <% end %>

notes_controller

before_action :set_problem
before_action :set_note, :except => [ :index, :new, :create]

def solution
    @note.update_attributes(note_params)
    redirect_to @problem
end


private
    # Use callbacks to share common setup or constraints between actions.
    def set_problem
      @problem = Problem.find(params[:problem_id])
      authorize @problem, :show?
    end

def set_note
@note = @problem.notes.includes(:user).find(params[:id])
  authorize @note
end

# Never trust parameters from the scary internet, only allow the white list through.
def note_params
  params.require(:note).permit(*policy(@note || Note).permitted_attributes).merge(:user => current_user)
end

note_policy.rb

def permitted_attributes
   if user.role == 'user'
    [ :body ]
   else
    [ :body, :time, :is_solution ]
   end
end

0 个答案:

没有答案