0

Purchase.rb というモデルがあります。各購入は、次のようなフォームから作成されます。

<%= form_for(@purchase) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :content, placeholder: "Describe something you are interested in buying.", :maxlength=>"254" %>
    <%= f.file_field :photo %>

  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

それらが作成されると、app/views/purchases/_purchase.html.erb に保存された部分とともに表示されます。写真を追加するために、私は入れました

<%= form_for(@purchase) do |f| %>
    <%= f.file_field :photo %>
    <%= f.submit "Post", class: "btn btn-large btn-primary" %>
  <% end %>

購入自体で。ペーパークリップを使用しています。つまり、ユーザーは購入の一部であるフィールドをクリックして、ビューに写真を追加できるということです。

私が得るエラーは言う

Missing template purchases/users#show, application/users#show with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/alex/rails_projects/tradespring!/app/views"

app/views/purchases/users#show ではなく、app/views/users#show を見たい

編集:

ユーザーコントローラーの表示アクションは次のとおりです。

class UsersController < ApplicationController


  def show
    @user = User.find(params[:id])
    @purchases= @user.purchases
    @sales= @user.sales
    @purchase=Purchase.new
    @sale=Sale.new
  end

ここにroutes.rbがあります

Tradespring::Application.routes.draw do
  resources :users do
    resources :pcomments
    resources :scomments
  end
  resources :sessions, only: [:new, :create, :destroy]
  resources :purchases do
    resources :pcomments
  end
  resources :sales do
    resources :scomments
  end

  get "static_pages/home"

  get "static_pages/about"

  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new' 
  match '/about',   to: 'static_pages#about'
  match '/signout', to: 'sessions#destroy', via: :delete

  root to: 'static_pages#home'

最後に、画像フォームを送信するときに私がやりたいことは次のとおりです。100% 更新中かどうかはわかりません。

class PurchasesController < ApplicationController
 def update
    @purchase = Purchase.find(params[:id])
    if @purchase.update_attributes(params[:purchase])
      flash[:success] = "Picture added"

      redirect_to :back
    else
      render 'users/show'
    end
  end

ここにも app/views/user/show.html.erb があります

<% provide(:title, @user.name) %>
<p>
<%= mail_to(@user.email, name="email this user", :encode => "javascript") %>
</p>

<div id="purchases">

<%= form_for(@purchase) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :content, placeholder: "Describe something you are interested in buying.", :maxlength=>"254" %>
    <%= f.file_field :photo %>
    <p1> Note: There is a 254 character limit. Be sure to include useful information such as product specifications, how much you are willing to pay, and shipping info (where you live, if you want to pick it up locally, ect.). Further detail is best left to email.</p1>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

<% if @user.purchases.any? %>
      <h3>Purchases (<%= @user.purchases.count %>)</h3>
      <ol class="purchases">
        <%= render @purchases %>
      </ol>
<% end %>
</div>


<div id="sales">

<%= form_for(@sale) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :content, placeholder: "Describe something you are interested in selling.", :maxlength=>"254" %>
    <p1> Note: There is a 254 character limit. Be sure to include useful information such as product specifications, price, payment methods accepted, and shipping info (where you live, if you are willing to ship it, ect.). Further detail is best left to email. </p1>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

<% if @user.sales.any? %>
      <h3>Sales (<%= @user.sales.count %>)</h3>
      <ol class="sales">
        <%= render @sales %>
      </ol>
<% end %>
</div>
4

1 に答える 1

0

問題は、2 番目のフォームのアクションを定義する必要があることでした。デフォルトでは、フォームは作成アクションが必要であると想定しています。この場合、更新アクションを使用したかったので、 <%= form_for((@purchase), :url => { :action => "update" }) do |f| に変更する必要がありました。%>

于 2012-11-08T09:20:31.667 に答える