简体   繁体   中英

rendering form, and display the input on the same page

so I'm trying to create a videogame review website for practice. each game would have it's own page, and on each page, i'd like to have a form where you can input reviews, and have it show up on the same page when submitted.

I'm not sure how to handle this. but I managed to create two controllers -- one for creating games, and one for creating reviews.

I use devise for user logins/registration

I'll post the codes below: please let me know if you need to see other files.

routes.rb

Rails.application.routes.draw do

  devise_for :users

  resources :games
  resources :reviews

  root "games#index"

end

games_controller.rb -- to create new games

class GamesController < ApplicationController

    before_action :set_game, only: [:show, :edit, :update, :destroy]

    def index
        @games = Game.all
    end

    def new
        @game = Game.new
    end

    def create
        @game = Game.create(game_params)
        redirect_to @game
    end

    def edit
    end

    def update
        @game.update(game_params)
        redirect_to @game
    end

    def destroy
    end

    private

    def game_params
        params.require(:game).permit(:title, :image, :developer, :genre, :release_date, :platform)
    end

    def set_game
        @game = Game.find(params[:id])
    end

end

reviews_controller.rb -- to create reviews

class ReviewsController < ApplicationController

    before_action :set_review, only: [:show, :edit, :update, :destroy]

    def index
    end

    def show
    end

    def new
        @review = Review.new
    end

    def create
        @review = Review.create(review_params)
        redirect_to @review
    end

    private

    def review_params
        params.require(:review).permit(:review)
    end

    def set_review
        @review = Review.find(params[:id])
    end

end

show.html.erb for games

<% if user_signed_in? %>
    <p class=""><%= link_to "Update Details", edit_game_path %></p>
    <p class=""><%= link_to "Update News", new_game_path %></p>
<% end %>

<%= flash[:notice] = "Successfully updated." %>
<hr>
<div class="game_summary">
    <%= image_tag @game.image %>
    <div class="game_details">
        <h3 class="game_title"><%= @game.title %></h3>
        <ul class="game_info">
            <li>Developer: <%= @game.developer %></li>
            <li>Release Date: <%= @game.release_date %></li>
            <li>Genre: <%= @game.genre %></li>
            <li>Platform(s): <%= @game.platform %></li>
        </ul>
        <button class="buttons" id="buy">Buy</button>
        <button class="buttons" id="rent">Rent</button>
    </div>
</div>

<div class="game_news">
    <h2 class="game_news_title">News & Articles</h2>
</div>

<div class="game_reviews">
    <%= link_to "Add Review", new_review_path %>    
    <h2>Reviews</h2>
    <ul>
        <li><%= %></li>
        <li><%= %></li>
        <li><%= %></li>
        <li><%= %></li>
    </ul>
</div>

In your games_controller.rb , you'd need a show method (which is created by default when you use scaffolding, but which you somehow missed). This show method would gather all the reviews pertaining to the game in question, assuming your Review model has a reference to its corresponding Game model (both as a foreign key specified in schema.rb and via belongs_to and has_many in their respective models).

The show method would look like this:

def show
  @reviews = Review.where("game_id = ?", @game.id)
end

Then in your view, you'd have something like this:

<% @reviews.each do | review | %>
  <%= review.title %>
  <%= review.content %>
<% end %>

I have no idea what fields are contained in your Review object, but you can extrapolate from this example to get what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM