ruby on rails - RoR: Param is missing or the value is empty -
i getting error when try update prop(which article). can create prop when go edit following error:
this props controller:
class propscontroller < applicationcontroller attr_accessor :user, :answer, :choice, :prop
def index @props=prop.all end def show @prop = prop.find(params[:id]) end def new @prop = prop.new @user = user.find(session[:user_id]) end def edit @prop = prop.find(params[:id]) @user = user.find(session[:user_id]) @answer = @user.answers.update(prop_params) end def create @prop = prop.new(prop_params) @user = user.find(session[:user_id]) @answer = answer.new if @prop.save redirect_to @prop else render 'new' end end def update @user = user.find(session[:user_id]) @prop = prop.find(params[:prop_id]) @answer = @user.answers.update(answer_params) if @prop.update(prop_params) redirect_to @prop else render 'edit' end if @answer.choice == @prop.choice puts "hello" @user.score += 7 @user.save else @user.score -= 7 @user.save end end def destroy @prop = prop.find(params[:id]) @prop.destroy redirect_to props_path end def select @prop = prop.find(params[:choice]) end end private def prop_params params.require(:prop).permit(:title, :text, :choice, :user_id, :id) end def answer_params params.require(:answer).permit(:choice, :id, :prop_id, :user_id) end
this form. partials shared between new , edit views:
<%= form_for @prop |f| %> <% if @prop.errors.any? %> <div class="error_explanation"> <h2> <%= pluralize(@prop.errors.count, "error") %> prohibited prop being saved: </h2> <ul> <% @prop.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <p> <%= f.label :title %><br> <%= f.text_field :title %> </p> <p> <%= f.label :text %><br> <%= f.text_area :text %> </p> <%= f.radio_button(:choice, "a") %> <%= f.label(:choice, "the correct answer a") %> <%= f.radio_button(:choice, "b") %> <%= f.label(:choice, "the correct answer b") %> <%= f.radio_button(:choice, "c") %> <%= f.label(:choice, "the answer has yet determined") %> <%= f.hidden_field :user_id, :value => @user.id %> <p> <%= f.submit %> </p> <% end %>
can please change update action as:
def update @user = user.find(session[:user_id]) @prop = prop.find(params[:prop_id].merge(:user_id => @user)) @answer = @user.answers.update(answer_params) if @prop.update(prop_params) redirect_to @prop else render 'edit' end if @answer.choice == @prop.choice puts "hello" @user.score += 7 @user.save else @user.score -= 7 @user.save end end
and prop params as:
def prop_params params.require(:prop).permit(:title, :text, :choice, :id) end
this helps in sending user_id prop table using merge method. try hope resolve issue , make sure change create action send user_id prop table.
Comments
Post a Comment