Ruby on Rails First argument in form cannot contain nil or be empty -


i used standard ruby on rails generate scaffolding. need find out how move form "new" view "posts" view (main page). keep on getting error "first argument in form cannot contain nil or empty". here "posts" view code. works fine in "new" view not "posts" view.

posts view:

<%= form_for(@post) |f| %> 

new view:

<h1>new post</h1>  <%= render 'form' %>  <%= link_to 'back', posts_path %> 

form view:

<%= form_for(@post) |f| %>   <% if @post.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@post.errors.count, "error") %> prohibited post being saved:</h2>        <ul>       <% @post.errors.full_messages.each |message| %>         <li><%= message %></li>       <% end %>       </ul>     </div>   <% end %>     </div>     <div class="field">     <%= f.label :casino %>     <%= f.text_field :casino %>   </div>      </div>     <div class="field">     <%= f.label :city %>     <%= f.text_field :city %>   </div>   </div>      </div>     <div class="field">     <%= f.label :state %>     <%= f.text_field :state %>   </div>        </div>     <div class="field">     <%= f.label :country %>     <%= f.text_field :country %>   </div>   <div class="field">     <%= f.label :game %>     <%= f.text_field :title %>   <div class="field">     <%= f.label :minimum %>     <%= f.text_field :text %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %> 

controller code:

class postscontroller < applicationcontroller    before_action :set_post, only: [:show, :edit, :update, :destroy]    # /posts   # /posts.json   def index     @posts = post.all   end    # /posts/1   # /posts/1.json   def show   end    # /posts/new   def new     @post = post.new   end    # /posts/1/edit   def edit   end    # post /posts   # post /posts.json   def create     @post = post.new(post_params)      respond_to |format|       if @post.save         format.html { redirect_to @post, notice: 'post created.' }         format.json { render :show, status: :created, location: @post }       else         format.html { render :new }         format.json { render json: @post.errors, status: :unprocessable_entity }       end     end   end    # patch/put /posts/1   # patch/put /posts/1.json   def update     respond_to |format|       if @post.update(post_params)         format.html { redirect_to @post, notice: 'post updated.' }         format.json { render :show, status: :ok, location: @post }       else         format.html { render :edit }         format.json { render json: @post.errors, status: :unprocessable_entity }       end     end   end    # delete /posts/1   # delete /posts/1.json   def destroy     @post.destroy     respond_to |format|       format.html { redirect_to posts_url, notice: 'post destroyed.' }       format.json { head :no_content }     end   end    private     # use callbacks share common setup or constraints between actions.     def set_post       @post = post.find(params[:id])     end      # never trust parameters scary internet, allow white list through.     def post_params       params.require(:post).permit(:title, :text, :casino, :city, :state, :country)     end end 

add line in index action of postscontroller

@post = post.new 

this should solve problem :)

solution:

class postscontroller < applicationcontroller   before_action :set_post, only: [:show, :edit, :update, :destroy]    # /posts   # /posts.json   def index     @posts = post.all     @post = post.new    end 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -