mattC
mattC

Reputation: 373

Rails routing : nested resources

I'm running into a problem when trying to create a new object using nested resources in Rails. My routing is set up as:

resources :coins do
    resources :questions
end

When I attempt to create a new question, it does not save. I'm redirected to the 'questions' page and the form from the 'new' page including everything that was typed into it remains on the page (rather than the list of questions that are supposed to be there when it saves). My controller is as follows:

class QuestionsController < ApplicationController

  before_action :find_question, only: [:show]
  before_action :find_coin
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @questions = Question.where(coin_id: @coin.id).order("created_at DESC")
  end

  def show
  end

  def new
    @coin
    @question = current_user.questions.build
  end

  def create
    @question = current_user.questions.build(question_params)
    if @question.save
        redirect_to coin_question(@question.coin_id, @question.id)
    else
      render 'new'
    end
  end

  private

    def find_question
        @question = Question.find(params[:id])
    end

    def find_coin
      @coin = Coin.find(params[:coin_id])
    end

    def question_params
        params.require(:question).permit(:content, :ques_num, :coin_id)
    end

end 

My 'new' page then displays the following form:

<%= simple_form_for @question, url: coin_questions_path(@coin.id) do |f| %>
    <%= f.input :ques_num %>
    <%= f.input :content %>
    <%= f.submit "Post", class: "btn btn-primary" %>
<% end %>

This is my first time using nested resources and its tripping me up a little bit. I really appreciate any assistance here.

Upvotes: 0

Views: 45

Answers (2)

random_user_0891
random_user_0891

Reputation: 2061

Your create action is failing and so it's executing the else statement which is just rendering back your form with the data you entered. The easiest thing to do is to just check out the log file and see why the save it being blocked.

go to /log/development.log and if you're using a mac press Command and the down arrow which will bring you all the way to the bottom of the file.

Also you may want to check out your model validations. If you don't have flash setup or aren't outputting the errors to your view a validation may be causing the form not to save and you wouldn't see the errors.

you could add some error handling to your view like this

  <%= form_with(model: question, local: true) do |form| %>
    <% if question.errors.any? %>
      <div id="error_explanation">
        <h2 class="text-danger"><%= pluralize(question.errors.count, "error") %> prohibited this question from being saved:</h2>

        <ul class="text-danger">
        <% question.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
        </ul>
      </div>
    <% end %>

for your controller try

def create
  @question = Question.new(question_params)   
  if @question.save
    flash[:success] = "question created successfully!"
    redirect_to question_url(@question.id)
  else
    render 'new'
  end
end 

Upvotes: 1

ashvin
ashvin

Reputation: 2040

I think there will be error to create question.

if @question.save
  redirect_to coin_question(@question.coin_id, @question.id)
else
  render 'new'
end

So if record have any error to save it will redirect to new form.

Just use following code to know what is the errors to creating question

def create
  @question = current_user.questions.build(question_params)
  if @question.save
    flash[:notice] = 'Question created'
    redirect_to coin_question(@question.coin_id, @question.id)
  else
    flash[:notice] = 'Some error here!'
    render 'new'
  end
end

You need to setup flash to show flash error.

Upvotes: 0

Related Questions