I am currently creating an app with Ruby on Rails and would like to be able to post and save notes and photos using form_with.
When I set up an association, created a table, and posted it, the following error occurred.
I want to resolve this error so that I can save the posted data.
ActiveModel :: MissingAttributeError in ItemsController # new can't write unknown attribute `post_id` Extracted source (around line # 12): def new @item = Item.new @ item.build_post @post = Post.new (post_params) end Application Trace | Framework Trace | Full Trace app/controllers/items_controller.rb: 12: in `new' Request Parameters: None Toggle session dump Toggle env dump Response Headers: None
class ItemsController @ post.img_identifier) end private def item_params params.require (: item) .permit (: image,: memo, post_attributes: [: id ,: img ,: item_id]). merge (user_id: current_user.id) end def set_item @item = Item.find (params [: id]) end def move_to_index unless user_signed_in? redirect_to action :: index end end def post_params params.permit (: img) end end
Supplementary information (FW/tool version, etc.)
class Item</pre> <strong>models/post.rb</strong> <pre><code>class Post</pre> <strong>view/items/_form.html.haml</strong> <pre><code>= form_with (model: item, local: true) do | form | = form.text_area: memo, pleaceholder: "memo", rows: "10" = form.file_field: img = form.submit "SEND"
I am creating an app by trial and error on my own, and there may be some mistakes such as how to set up an association.
We are not currently working on it, so we would appreciate it if you could give us some advice.
I look forward to working with you.
Answer # 1
By changing the place where belongs_to: post in item.rb of model to has_one: post, the error was improved and I was able to post.Reason for error
Since belongs_to means to be subordinate, and has_one seems to have one target, so be careful when incorporating Assiciation after that.
- ruby - when posting an image, an error occurs where the image is not defined
- ruby - i can't save posts with form_with in rails 60
- ruby - [rails] when registering new data in form_with, nomethoderror appears and the process fails
- ruby on rails - save the item selected in the select box of rails form_with
- ruby - [rails] when using form_with, an argumenterror occurs and it is said that the arguments do not match
- ruby - i want to display the comment posting column with rails so that the comment list can be seen
- ruby - image data is not registered in db by carrier wave image posting (not assigned to instance variable)
- ruby - i am stumbling on the association between the tables!
- ruby - after posting an article on rails simple calendar, i want to reflect it on the calendar
- ruby - if statement and hidden_field_tag in form_with
- ruby - the header posting screen does not open only when transitioning from my page
- ruby on rails 5 - please tell me how to combine tables after rails group_by
- ruby - [rails] i want to use fields_for with form_with, but an error occurs and the page cannot be displayed
- ruby on rails - [rails] form_with submit button does not respond
- ruby - about form_with of doubly nested routing
- ruby on rails 5 - some items are not saved in db when posting from rails form
- ruby - nomethoderror in posts # new in form_with
- ruby - use form_with in rails to specify multiple models and save values
- ruby on rails - i cannot save a comment on the comment posting page
- ruby - i get an error in the view while implementing carriewave
- ruby - i get an error saying undefined method `resize_to_limit'for
- ruby - nameerror uninitialized constant drinkscontroller :: drinktag
- ruby - tags are not saved in the database
- ruby - i don't understand the meaning of "redis:" described in yml file etc when dealing with redis in rails
- html - about boot strap free introduction
- ruby - i don't feel that the sql issued by rails has a different format from the sql that i usually write
- ruby - validation does not work
- ruby - mysql2 :: error: field'tag_name' doesn't have a default value