Home>

I'm working on a basic book output app with ruby ​​on rails.
The post function is implemented by linking the output model, user model, and library model.

I want to solve

Saved in the DB when the create action is invoked
Enter information and press the post button to jump to the next page
(Current status → It goes to else depending on the definition of if statement.)

Corresponding source code
app/controllers/libraries_controller.rb

   def new
   @library = Library.new
  end
  def create
    @library = Library.new (library_params)
    if @ library.save
      redirect_to root_path
    else else
      render: new
    end
  end

  private
  def library_params
    params.require (: library) .permit (: title,: impressions,: category_id,: lank_id,: author,: syuppan,: read_id)
  end
end
config/routes, rb
Rails.application.routes.draw do
  devise_for: users
  root to: "outputs # index"
  resources: users, only: [: edit,: update]
  resources: libraries, only: [: new,: create] do
    resources: outputs, only: [: index,: create]
  end
end
app/model/library, rb
class Library</pre>
<pre><code>db/migrate
class CreateLibraries</pre>
<pre><code>app/views/libraries/new.html.erb   Book registration form <% = form_with model: @library, local: true do | f |%>              <% = f.label: Book title, class:'chat-library-form__label'%>                <% = f.text_field: title, class:'chat__library_name chat-library-form__input', placeholder:'Please enter your book title'%>                        <% = f.label: Output, class:'chat-library-form__label'%>                <% = f.text_field: impressions, class:'chat__library_name chat-library-form__input', placeholder:'Enter output'%>                            <% = f.label: Category, class:'chat-library-form__label'%>                <% = f.text_field: category_id, class:'chat__library_name chat-library-form__input', placeholder:'Enter output'%>                                <% = f.label: Rank, class:'chat-library-form__label'%>                <% = f.text_field: lank_id, class:'chat__library_name chat-library-form__input', placeholder:'Enter output'%>                                <% = f.label: Author, class:'chat-library-form__label'%>                <% = f.text_field: author, class:'chat__library_name chat-library-form__input', placeholder:'Enter output'%>        
                    <% = f.label: Publisher, class:'chat-library-form__label'%>          <% = f.text_field: syuppan, class:'chat__library_name chat-library-form__input', placeholder:'Enter output'%>                            <% = f.label: Where you read, class:'chat-library-form__label'%>                <% = f.text_field: read_id, class:'chat__library_name chat-library-form__input', placeholder:'Enter output'%>                               <% = f.submit class:'chat-library-form__action-btn'%>        <% end%>


Console after pressing the register button

 (2.5ms) SET NAMES utf8, @@ SESSION.sql_mode = CONCAT (CONCAT (@@ sql_mode,', STRICT_ALL_TABLES'),', NO_AUTO_VALUE_ON_ZERO'), @@ SESSION.sql_auto_is_null = 0, @@ SESSION.wait_timeout = 2147483
   (0.4ms) SELECT `schema_migrations` .`version` FROM `schema_migrations` ORDER BY` schema_migrations` .`version` ASC
Processing by LibrariesController # create as HTML
  Parameters: {"authenticity_token" =>"kGujcXLmPiMvI28HP/bTLbOOLabiRkLkpiqElu726SQ1HT + U/KzJLQ3OScCgYq + rK + naGQTUF28LMDVaJn2sEg ==", "library" =>{"title" == "," library "=>{"title "=" "=>" aaa "," lank_id "=>" aaa "," author "=>" aaa "," syuppan "=>" aaa "," read_id "=>" aaa "},

 "commit" =>"Create Library"}
  User Load (0.5ms) SELECT `users`. * FROM` users` WHERE `users`.` id` = 1 ORDER BY `users`.` id` ASC LIMIT 1
  Rendering libraries/new.html.erb within layouts/application
  Rendered libraries/new.html.erb within layouts/application (Duration: 15.5ms | Allocations: 3394)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 1180ms (Views: 1087.9ms | ActiveRecord: 10.1ms | Allocations: 1561154)
What I tried

The class name etc. are a little different because it is based on other code.
In addition, category_id, rank_id, and read_id will be implemented using integer type in the future.

def create
    binding.pry → ① gem installed
    @library = Library.new (library_params)
    if @ library.save
      redirect_to root_path
    else else
      render: new
    end
  end

→ params
=>"qug7w4lniKwERgrbq0Fw2 + r/qtn5L0p0E1yepByr69sPnqcmBy1/oiarLBw01QxdcphdZh + 9H/++ Ri9o1CCu7Q ==", "library" == " >"aaa", "category_id" =>"aaa", "lank_id" =>"aaa", "author" =>"aaa", "syuppan" =>"aaa", "read_id" =>"aaa"} ,,

 "commit" =>"Create Library", "controller" =>"libraries", "action" =>"create"} permitted: false>→ library_params
=>"aaa", "impressions" =>"aaa", "category_id" =>"aaa", "lank_id" =>"aaa", "author" =>" aaa "," syuppan "=>" aaa "," read_id "=>" aaa "} permitted: true>
  • Answer # 1

    It is solved.
    The logged-in user was not defined.
    Therefore @ library.user_id = current_user.id
    Added.

  • Answer # 2

    It looks like it will not be saved because the user_id is not specified.
    From around rails5 belongs_to is required: true by default.

    To see why it isn't savedsave saveTosave!And give an error
    with prysave saveOrvalid?Aftererrors errorsIt is good to check.

    @ library.valid? # =>false
    @ library.errors

  • Answer # 3

    It is solved.
    The logged-in user was not defined.
    Therefore @ library.user_id = current_user.id
    Added.