Home>

I'm creating a simple message board, but I'm having trouble posting it.
What I want to achieve is that after posting a message, I want to display the flash while displaying play.html.erb that is displaying the form, and reflect it in the message list of the form. (Image of reloading the same page)
For the time being, I want to post the values ​​of the content and course columns of the messsage table.
Currently, even if you click "Post", neither screen transition nor flash is displayed, and the database is not reflected.
Thank you.

# routes.rb
Get'/ rounds/select_course', to:'rounds # select_course', as: "select_course"
  post "/ rounds/search", to:'rounds # search'
  get'/ rounds/search /: course', to:'rounds # show', as: "round_start"
  get'/ rounds/play /: course /: round_id', to:'rounds # play', as: "round_play"
  post'/ rounds/play /: course /: round_id', to:'rounds # message_post', as: "round_message_post"
  get "rounds/input /: round_id /: hole_number", to:'rounds # input', as:'hole_score_input'
  post "rounds/input /: round_id /: hole_number", to:'rounds # update', as:'hole_score_new'
# rounds.controller.rb
Class RoundsController  "All regions",
            101 =>"All about Hokkaido and Tohoku",
            1 =>"Hokkaido",
            2 =>"Aomori Prefecture",
            3 =>"Iwate Prefecture",
            4 =>"Miyagi Prefecture",
            5 =>"Akita Prefecture",
            6 =>"Yamagata Prefecture",
            7 =>"Fukushima Prefecture",
            102 =>"Everything in Kanto",
            8 =>"Ibaraki",
            9 =>"Tochigi Prefecture",
            10 =>"Gunma",
            11 =>"Saitama Prefecture",
            12 =>"Chiba",
            13 =>"Tokyo",
            14 =>"Kanagawa",
            19 =>"Yamanashi",
            20 =>"Nagano",
            22 =>"Shizuoka Prefecture",
            103 =>"All about Hokuriku",
            15 =>"Niigata",
            16 =>"Toyama",
            17 =>"Ishikawa",
            18 =>"Fukui Prefecture",
            104 =>"All about Chubu",
            21 =>"Gifu Prefecture",
            23 =>"Aichi",
            24 =>"Mie Prefecture",
            105 =>"All about Kinki",
            25 =>"Shiga",
            26 =>"Kyoto",
            27 =>"Osaka",
            28 =>"Hyogo",
            29 =>"Nara",
            30 =>"Wakayama Prefecture",
            106 =>"All about China",
            31 =>"Tottori Prefecture",
            32 =>"Shimane Prefecture",
            33 =>"Okayama Prefecture",
            34 =>"Hiroshima",
            35 =>"Yamaguchi Prefecture",
            107 =>"All about Shikoku",
            36 =>"Tokushima Prefecture",
            37 =>"Kagawa Prefecture",
            38 =>"Ehime Prefecture",39 =>"Kochi Prefecture",
            108 =>"All about Kyushu and Okinawa",
            40 =>"Fukuoka",
            41 =>"Saga",
            42 =>"Nagasaki",
            43 =>"Kumamoto Prefecture",
            44 =>"Oita Prefecture",
            45 =>"Miyazaki Prefecture",
            46 =>"Kagoshima Prefecture",
            47 =>"Okinawa Prefecture",
            109 =>"Overseas"
          }
  end
  def search
        require'net/http'
        require'json'
        require'uri'
        @round_id = params [: golfCourseName]
         @todoufukens = {
          0 =>"All regions",
          101 =>"All about Hokkaido and Tohoku",
          1 =>"Hokkaido",
          2 =>"Aomori Prefecture",
          3 =>"Iwate Prefecture",
          4 =>"Miyagi Prefecture",
          5 =>"Akita Prefecture",
          6 =>"Yamagata Prefecture",
          7 =>"Fukushima Prefecture",
          102 =>"Everything in Kanto",
          8 =>"Ibaraki",
          9 =>"Tochigi Prefecture",
          10 =>"Gunma",
          11 =>"Saitama Prefecture",
          12 =>"Chiba",
          13 =>"Tokyo",
          14 =>"Kanagawa",
          19 =>"Yamanashi",
          20 =>"Nagano",
          22 =>"Shizuoka Prefecture",
          103 =>"All about Hokuriku",
          15 =>"Niigata",
          16 =>"Toyama",
          17 =>"Ishikawa",
          18 =>"Fukui Prefecture",
          104 =>"All about Chubu",
          21 =>"Gifu Prefecture",
          23 =>"Aichi",
          24 =>"Mie Prefecture",
          105 =>"All about Kinki",
          25 =>"Shiga",
          26 =>"Kyoto",
          27 =>"Osaka",
          28 =>"Hyogo",
          29 =>"Nara",
          30 =>"Wakayama Prefecture",
          106 =>"All about China",
          31 =>"Tottori Prefecture",
          32 =>"Shimane Prefecture",
          33 =>"Okayama Prefecture",
          34 =>"Hiroshima",
          35 =>"Yamaguchi Prefecture",
          107 =>"All about Shikoku",
          36 =>"Tokushima Prefecture",
          37 =>"Kagawa Prefecture",
          38 =>"Ehime Prefecture",
          39 =>"Kochi Prefecture",108 =>"All about Kyushu and Okinawa",
          40 =>"Fukuoka",
          41 =>"Saga",
          42 =>"Nagasaki",
          43 =>"Kumamoto Prefecture",
          44 =>"Oita Prefecture",
          45 =>"Miyazaki Prefecture",
          46 =>"Kagoshima Prefecture",
          47 =>"Okinawa Prefecture",
          109 =>"Overseas"
        }
        base_url = "https://app.rakuten.co.jp/services/api/Gora/GoraGolfCourseSearch/20170623"
        k = params [: keyword]
        t = params [: todoufuken]
        params = {
          'keyword' =>URI.encode (k),
          'areaCode' =>t,
          'format' =>'json',
          'applicationId' =>'1083401508369226218',
          'hits' =>30
        }
        uri = URI (base_url +'?' + params.map {| k, v | "# {k} = # {v}"} .join ('&'))
        puts uri
        #begin
        response = Net :: HTTP.start (uri.host, uri.port, use_ssl: uri.scheme =='https') do | http |
          http.open_timeout = 5
          http.read_timeout = 10
          http.get (uri.request_uri)
        end
        #case response
          # when Net :: HTTPS Success
            @json = JSON.parse (response.body)
        # end
        # rescue =>e
        # Rails.logger.error (e.message)
        # raise e
        # end
  end
  def show # 1 to 18 hours Post the name of the golf course and generate a record (hole_score is Null)
    @coursename = params [: golfCourseName]
    @round_id = Message.where (course: params [: course])
    round_id = SecureRandom.hex (8)
    # hoge = params [: round_id]
    [* 1..18] .each do | num |
      Score.create! (Course: params [: course],

hole_number: num, user_id: 1, round_id: round_id, hole_score: nil)
    end
    @round_id = round_id
    redirect_to round_play_path (course: params [: course],

 round_id: @round_id)
  end
  def play
      @coursename = params [: round_id]
      @score_card_score = Score.where (round_id: params [: round_id]) # Get score for each hole
      @score_card_course = Score.where (round_id: params [: round_id]). first # Get rounded course, date
      @score_sum = Score.where (round_id: params [: round_id]). sum (: hole_score)
      #Ranking relationship
      today_same_course = Score.where ("created_at>=?", Date.today) .where (course: params [: course])
      @over_under = today_same_course
        .group (: round_id)
        .group (: course)
        .group (: user_id)
        .sum (: hole_score)# .select ("user_id, sum (hole_score --par) as overunder") .order ("overunder")
    # @Sum_score = Score.group (: round_id) .select ("round_id, sum (hole_score) as sum_score"). Order ("sum_score")
    #Message related
    @message_course = params [: course]
    @course_params = params [: course]
    # @mmm = Message.find_by ("course: course_params")
    @messages = Message.where (course: params [: course])
  end
  def input
      @hole_number = params [: hole_number]
      @round_id = params [: round_id]
      @score = Score.find_by (round_id: params [: round_id],

 hole_number: params [: hole_number])
    end
  def update
      @score = Score.find_by (round_id: params [: round_id],

 hole_number: params [: hole_number])
      if @ score.update (hole_score: params [: hoge])
          flash [: edit_success] ='The score has been edited'
          redirect_to round_play_path (course: @ score.course, round_id: params [: round_id])
      else else
          flash.now [: danger] ='Score was not edited'
          render'archives/show'
      end
  end
  def message_post
    @message = Message.new (message_params)
    if @ message.save (cotent: params [: containt],

 course: params [: course])
      flash [: message_post_success] ='Message posted'
      redirect_to round_play_path (course: @ score.course, round_id: params [: round_id])
    else else
      flash.now [: danger] ='Score was not edited'
    end
  end
  #Strong Parameter
  def message_params
    params.require (: @message) .permit (: content)
  end
end
# play.html.erb<% = @ score_card_course.course%>message board<% = form_with url: round_message_post_path do | f |%><% = f.label: content,'message'%><% = f.text_field: content%><% = f.submit "submit"%><% end%><% @ messages.each do | m |%><span><% = m.content%></span><span><% = m.created_at%></span><span>Posted by<% = m.user.name%>san</span><br><% end%>
Log when "Post" is clicked
Started POST "/ rounds/play /% E5% 8B% 9D% E7% 94% B0% E3% 82% B4% E3% 83% AB% E3% 83% 95% E5% 80% B6% E6% A5% BD % E9% 83% A8/814ffb7eed39c15d "for :: 1 at 2020-11-17 09:34:57 +0900
Processing by RoundsController # message_post as JS
  Parameters: {"authenticity_token" =>"/ T + BTAttpIP5Xa4/muTbH + y/4utPzFt8REnOWdEnF63U8f0dp3c1zAYOztz/D7f0CtqRqNAlJCJI7fI3NBYPQA ==", "content" =>"com" =="," content "=>" Katsuta Golf Club "," round_id "=>" 814ffb7eed39c15d "}
Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms | Allocations: 738)

ActionController :: ParameterMissing (param is missing or the value is empty: @message):
app/controllers/rounds_controller.rb: 234: in `message_params'
app/controllers/rounds_controller.rb: 220: in `message_post'
  • Answer # 1

    There was a typo, but it was resolved.

  • Answer # 2

    There are various problems.
    It's hard to make a mistake in using the standard method, but since there are many corrections from the current code, I will answer in the direction of going as it is.

    Check the log to see Paramater. The structure is different from what message_params expects. Since it is troublesome, I will match it with message_params
    params.permit (: content)To

    if @ message.save (cotent: params [: containt], course: params [: course])Toif @ message.saveTo

    If you still can't save with this, temporarilyif @ message.save!And show me the result