2016-04-06 16 views
0

Wenn ich dieses Formular absende, wird ein neuer Datensatz erstellt, aber die Werte werden nicht hinzugefügt. Versucht ein paar andere Dinge, aber nichts funktioniertform_for erstellt einen neuen Datensatz, aber die Werte sind leer

Kommentare Controller

def report 

    @report_comment = ReportComment.new 
    if @report_comment.save(report_comment_params) 

     redirect_to root_path 
    else 
     render 'static_pages/home' 
    end 
    end 

private 
    def report_comment_params 
    params.require(:report_comment).permit(:guide_id, :submitted_by, :comment_id) 

Ende

Kommentare Ansicht (nur die Berichtsformular)

<% @report_comment = ReportComment.new %> 
<% @guide = Guide.friendly.find(params[:guide_id]) %> 
<% if current_user.nil? %> 
<% user_id = 0 %> 
<% else %> 
<% user_id = current_user.id %> 
<% end %> 
      <%= form_for([@report_comment], url: comments_report_path, method: :post) do |f| %> 
     <%= f.hidden_field :guide_id, value: @guide.id %> 
     <%= f.hidden_field :submitted_by, value: user_id %> 
     <%= f.hidden_field :comment_id, value: comment.id %> 

     <%= f.submit "Report" %> 

    <% end %> 

log mit den übergebenen params

Parameters: {"utf8"=>"✓", "authenticity_token"=>"0XMjwzMlMLSwqpImXpjTRWd8yHUP/ERTkvnzd9rWIkSA94l9f9bCQdODXrC3SyvmfJjuW/N3zkp5pVZAf+0D+w==", "report_comment"=>{"guide_id"=>"1", "user_id"=>"1", "comment_id"=>"1"}, "commit"=>"Report"} 

Nicht sicher, warum die Werte nicht gespeichert werden. Ich kann alternativ hinzufügen @report_comment.update(guide_id: params[:report_comment][:guide_id], submitted_by: params[:report_comment][:submitted_by], comment_id: params[:report_comment][:comment_id]) unter if @report_comment.save(report_comment_params) aber das ist nicht ideal und nicht so gut wie nur sie direkt aus dem Formular speichern speichern.

+1

'@report_comment = ReportComment.new (report_comment_params)' lösen wird. –

Antwort

1

zuordnen params neue Methode und speichern Sie es dann

def report 
    @report_comment = ReportComment.new(report_comment_params) 
    if @report_comment.save 
    redirect_to root_path 
    else 
    render 'static_pages/home' 
    end 
end 
+0

Danke. Ich habe es millionenfach gemacht, bin aber nur ein bisschen in die Verlegenheit gegangen, haha. Trotzige brauchen etwas Schlaf – Rob

0

Pass params neue

@report_comment = ReportComment.new(report_comment_params) 
if @report_comment.save 

    redirect_to root_path 
else 
    render 'static_pages/home' 
end 
0

Sie Controller falsch ist, sollten Sie die Parameter in ReportComment.new nicht in .save passieren:

def report 

    @report_comment = ReportComment.new(report_comment_params) 
    if @report_comment.save 
    redirect_to root_path 
    else 
    render 'static_pages/home' 
    end 
end 

Und viel besser, wenn Sie Validierungen in Ihrem Mod hinzufügen el.

1

Übergeben Sie den Parameterwert an .new Instantiate-Methode. Sie können nicht params passieren Methode

def report 
    @report_comment = ReportComment.new(report_comment_params) 
    if @report_comment.save 
    redirect_to root_path 
    else 
    render 'static_pages/home' 
    end 
end 

zu speichern oder Sie können Methode Benutzer erstellen, anstatt speichern

def report 
    @report_comment = ReportComment.create(report_comment_params) 
    if @report_comment.present? 
    redirect_to root_path 
    else 
    render 'static_pages/home' 
    end 
end 
Verwandte Themen